Hello:
I have a array like:int MyArray[n][m];
I can't know the value of 'n' and 'm'.They're
must had run the program,get the two value.
I want use point but there was a question.
I need help!
Welcome any advice!
Thank you.
Printable View
Hello:
I have a array like:int MyArray[n][m];
I can't know the value of 'n' and 'm'.They're
must had run the program,get the two value.
I want use point but there was a question.
I need help!
Welcome any advice!
Thank you.
Try this:
int **MyArray;
Somewhere in the program:
for(int i(0);i<n;i++)
MyArray(i) = new int (m);
Dont forget to delete your memory,
ric
For a dynamic array at run time, try this :
// to allocate (assume type int for array)
int *pArray ;
pArray = new int[x_array_size * y_array_size] ;
// to access using indexes i for x and j for y
value = pArray[i + j * y_array_size] ;
// to delete array
delete []pArray ;
You will have to deal with making sure that the values of i,j do not go outside the array bounds that you allocate.
HTH
Roger Allen
I suppose you've noticed the mistake? To get a m*n array do the following:
int **MyArray;
MyArray = new int*[m];
for (int x = 0; x < m; i++)
{
MyArray[x] = new int[n];
}
int value_at_x_y = MyArray[x][y];
Then, don't forget to delete everything:
for (int x = 0; x < m; x++)
{
delete MyArray[x];
}
delete MyArray;
Tada,
Oliver.
OK, you are right, but I run my code on a Borland 3.1 compiler and it doesnot spit out any "Null Pointer
Assignment" so I supposed that it works.
Of course, we must alloc first for the pointers then for ints.
Thanks
A bug!
The line
delete MyArray ;
// should read
delete []MyArray ;
I thought I taught you better than that Oli!
(ps I work with him)
Roger Allen
Hello:
Thank you very much.
I receive your message.Now My Array like this:
MyArray[m][8];
In fact the value of 'm',I can't get when
the program start run.The value can get when
open file message is selected.(I use VC++6.0 MFC)
Welcome any advice!
Thank you very much.