Let’s say I have 2 2-dimensional arrays:
int a1[][2] = { {1,2}, {3,4}, {5,6} };
int a2[][2] = { {7,8}, {9,0}, {1,1} };
and a pointer:
int *a;
The pointer will point to one of the arrays and at some point point to the other (back and forth). After each reassignment of the pointer I want to read from the array, what is the easiest way to do that?
I can achieve what I want using the following way:
a = (int *)a1;
printf("D: %d\n", (int)(*a)+(x*2)+(y)));
a = (int *)a2;
printf("D: %d\n", (int)(*a)+(x*2)+(y)));
Output (assuming x = 0 and y = 1):
D: 2
D: 8
Is there another easier way to access the arrays I.e. by using the standard [] operator? If not, then how would you make this more “beautiful”… would you create a macro or a function or what would be the preferred way of doing it?
You can make use of
[]if you always have pairs and turn the pointer toThis should make it possible to write