2

When I run below code in MSVS, compiler gives

"Error 1 error C2059: syntax error : '{'

I am sure I am declaring and initializing double dimensional array right. Where is the syntax error?

#include <stdio.h>
#define STUDENTS 3
#define EXAM 4
void printArray(int array[][EXAM]);

int main(void){

int array[STUDENTS][EXAM];
array={ { 77, 68, 86, 73 },{ 96, 87, 89, 78 },{ 70, 90, 86, 81 } };
simonc
  • 41,632
  • 12
  • 85
  • 103
Lyrk
  • 1,936
  • 4
  • 26
  • 48

3 Answers3

4

You have to declare and initialize the array in a single statement.

int array[STUDENTS][EXAM]={ { 77, 68, 86, 73 },{ 96, 87, 89, 78 },{ 70, 90, 86, 81 } };

If you really need to initialize the array separately from its declaration, then you need to do it the hard way by setting each member individually.

array[0][0] = 77;
...
Graham Borland
  • 60,055
  • 21
  • 138
  • 179
3

In C99 you can make use of compound literals:

int (*array)[EXAM];
array = (int[STUDENTS][EXAM]){ { 77, 68, 86, 73 },{ 96, 87, 89, 78 },{ 70, 90, 86, 81 } };

EDIT: As Graham says: here, you aren't initializing a pre-declared array; you're initializing a new array, and then assigning its address to a pointer

David Ranieri
  • 39,972
  • 7
  • 52
  • 94
  • 4
    This may well be what the OP is looking for, but its semantics are subtly different from what the question asked. With the compound literal, you aren't initializing a pre-declared array; you're initializing a new array, and then assigning its address to a pointer. – Graham Borland May 03 '13 at 15:30
  • @GrahamBorland: And `sizeof array` doesn't work the way you might naively expect. – Keith Thompson May 03 '13 at 15:32
  • Comments are ephemeral; I suggest editing the information into your question. – Keith Thompson May 03 '13 at 15:33
2

array={ { 77, 68, 86, 73 ... }; is not valid syntax.

This type of expression should be used during initialization, like so:

int myArray[5] = { 0 };

Check out this question for a good overview of array initialization.

Community
  • 1
  • 1
eriknelson
  • 849
  • 2
  • 11
  • 21
  • Is this a must for all array declarations in C? or only for multiple subscripted arrays? – Lyrk May 03 '13 at 15:12
  • @user1939432 You don't have to use the initializer, but you can't use it in an assignment statement (this applies to all arrays). As Graham said in his answer, you'll have to initialize it using normal assignment syntax outside of its declaration. – eriknelson May 03 '13 at 15:16
  • 1
    No, you don't (if your compiler supports C99 compound literals); see David's answer. @user1939432: It applies to all arrays; multidimensional arrays are nothing more or less than arrays of arrays. – Keith Thompson May 03 '13 at 15:27
  • But for one dimensional arrays I can do this : int array2[STUDENTS]; for(i=0;i – Lyrk May 03 '13 at 15:30
  • @user1939432: Yes, and for two dimensional arrays you can do the same thing with a nested for loop. – Keith Thompson May 03 '13 at 15:31
  • (My english is bad sorry for misunderstanding) Then I can declare first and initialize later? But you said I have to declare and initialize all in one statement like this int array[STUDENTS][EXAM]={ { 77, 68, 86, 73 },{ 96, 87, 89, 78 },{ 70, 90, 86, 81 } }; – Lyrk May 03 '13 at 15:35