3

I am trying to assign certain members of a 2 dim array. Not able to get following code to work. Help would appreciated very very much.

int myArray[5][5] = {[1][1]{1},[2][2]{2},[3][3]{3},[4][4]{4}};

main()
{
  printf("%d %d\n", myArray[1][1], myArray[4][4]);
} 
Me Unagi
  • 615
  • 2
  • 7
  • 18
  • In what way does the code not work? Does it fail to compile, crash or give unexpected results? – simonc Sep 06 '13 at 16:58
  • I am getting following compile error array.c:3: error: expected ‘=’ before ‘{’ token array.c:3: error: expected ‘}’ before ‘[’ token – Me Unagi Sep 06 '13 at 17:01
  • haccks answer is only valid in c99: read:[Strange initializer expression?](http://stackoverflow.com/questions/18329206/strange-initializer-expression/18329258#18329258) – Grijesh Chauhan Sep 07 '13 at 08:02

1 Answers1

3

The way you are initializing the array

int myArray[5][5] = {[1][1]{1},[2][2]{2},[3][3]{3},[4][4]{4}};

is wrong. If you are interested in designator then initialize it as follows

int myArray[5][5] = {[1][1] = 1,[2][2] = 2,[3][3] = 3,[4][4] = 4};
haccks
  • 104,019
  • 25
  • 176
  • 264