How to allocate memory for 2-dimension array at run time ?
Printable View
How to allocate memory for 2-dimension array at run time ?
Think carefully before you answer this question: How many dimensions must be dynamic at runtime?
If the number of columns is fixed, while the number of row must vary (which is the case about 95% of the time), the answer is fairly simple.
If both the number of rows and the number of columns must vary, it's a lot more difficult.
Truth,
James
http://www.NJTheater.com
http://www.NovelTheory.com
I don't do it for the points (OK, maybe I do), but rating a post is a good way for me to know if I helped.
How about
int **array
int nRows=5;
int nCols=3;
array= new int*[nRows];
for ( int index = 0; index < nRows; index++ )
{
array[index] = new int[nCols];
}
Now is this what you were looking for?
Share what you know and ask what you dont....
Yes Saeed, I want to ask same thing but in different way. I do not want to specify any size of an array.
Suppose I have 'n' values to insert to an array, now at run time, program should automatically expand the size of an array and store new value.
You are better off looking at link lists
i suggest you look at
http://www.codeguru.com/cgi-bin/bbs/...&Number=296877
Share what you know and ask what you dont....
You don't give us any detail so I won't give you any :-) but if the standard C++ library doesn't have something for you (list, array, vector, etc.) then I'll go heave. Focusing on allocating memory worries me, are you sure you're thinking in C++ and not C? :)
THANKS for your KIND HELP.