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 } };
3
  • 2
    int variable[] = {foo, bar, baz} is only supported as an initializer, not as assignment. Commented May 3, 2013 at 15:08
  • Can't I declare first and then initialize in another place for arrays? Commented May 3, 2013 at 15:11
  • @user1939432 No, you can't, unless you initialize it the hard way by explicitly setting each entry. array[0][0] = 77; etc. Commented May 3, 2013 at 15:12

3 Answers 3

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;
...
Sign up to request clarification or add additional context in comments.

3 Comments

No, you don't (if your compiler supports C99 compound literals); see David's answer.
@KeithThompson subtly different semantics. 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.
Yes, you're right. You can't assign to an array at all. (I knew that, really I did!)
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

3 Comments

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.
@GrahamBorland: And sizeof array doesn't work the way you might naively expect.
Comments are ephemeral; I suggest editing the information into your question.
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.

6 Comments

Is this a must for all array declarations in C? or only for multiple subscripted arrays?
@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.
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.
But for one dimensional arrays I can do this : int array2[STUDENTS]; for(i=0;i<STUDENTS;i++) array2[i]=0;
@user1939432: Yes, and for two dimensional arrays you can do the same thing with a nested for loop.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.