On 11ÔÂ27ÈÕ, ÉÏÎç4ʱ51·Ö, Stephen.Schoenber...@gmail..com wrote:
I have declared a bunch of arrays and now I need to change the
dimensions on some of the arrays. For instance
int size = 1000;
int array1[size];
array1 is then filled with some values
now array1 dimensions need to be changed to size by 2 (2 columns) but
the orginial data needs to be retained, array just resized/
redimensioned. In some instances the dimensions need to be changed to
3.
Can this be done without completely recreating the array/deleting the
old one?
Thanks.
================================================== =================================================
Hi Stephen,
You cannot do direct convertion from one dimension to two.
See the below example:
int * array1 = new int[100];
int array3[10][10];
memset(array1,0,sizeof(int)*100);
int **array2 = (int **)array1;
array2 = (int**)array3;
array3[1][1]=10;
array2[1][1]=10;
You can see the assembly in VC05 compiler:
array3[1][1]=10;
00413BD2 mov dword ptr [ebp-174h],0Ah
array2[1][1]=10;
00413BDC mov eax,dword ptr [array2]
00413BE2 mov ecx,dword ptr [eax+4]
00413BE5 mov dword ptr [ecx+4],0Ah
The compiler will directly translate the two dimension array into
the single dimension array at compiling time. And for the pointer to
pointer array(**array2), if you treat it as a two dimension array, you
should manually copy the address of the 10 rows into the first 10
element of the array2[10][10].