I tried the following:
#include <stdio.h>
int main(void) {
char multi[3][10];
multi[0] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
multi[1] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'};
multi[2] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'};
printf("&multi[2][2]: %d \n", (int) &multi[2][2]);
printf("(*multi + 2) + 2: %d \n" , (int) (*multi + 2) + 2);
}
and yielded this output:
dominik@luna:~$ gcc tmp.c
tmp.c: In function ‘main’:
tmp.c:5: error: expected expression before ‘{’ token
tmp.c:6: error: expected expression before ‘{’ token
tmp.c:7: error: expected expression before ‘{’ token
I already did some research and found this thread where Vladimir stated “You should initialize your arrays in the same line you declare them”. This still leaves me confused, does that me you should not do it as in you should not write spaghetti code, or does it mean that you cannot do it.
Or could it be that I am doing something else completely wrong?
Arrays can be initialized, but not assigned, in this manner.
Also note that this will truncate the stack addresses that you print out, at least on 64-bit systems. Are you trying to print out stack addresses? Do this:
On my system, this change will cause it to print the correct addresses, which are just under 247 (above
0x7fff00000000), which is out of the range ofint.