PDA

View Full Version : Copying a pointer to a static 2D array



Cruz
10th April 2010, 19:27
Hello!

This is actually a question about C and not so much about Qt. I declare a static 2D array of ints like this:

int i[2][2];
i[0][0] = 1;
i[0][1] = 2;
i[1][0] = 3;
i[1][1] = 4;

And now I would like to have a second pointer j, that points to the same array.

int** j = i;

But the compiler doesn't allow me to do that: "cannot convert ‘int (*)[2]’ to ‘int**’ in initialization".
Trying to "hack" it with a memcpy

memcpy((void*)&j, (void*)i, sizeof(long int));

leads to a segmentation fault.

What am I not doing right?

Thanks,
Cruz

squidge
10th April 2010, 20:54
int *j = i;

j[0][0] = 1;
j[1][1] = 2;

or just j[3] = 4;

etc.

Archimedes
11th April 2010, 16:05
Or int (*j)[2] = i;
and then j[0][0] = 100;