Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does my array initialisation not compile? [duplicate]

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 } };
like image 904
Lyrk Avatar asked Feb 10 '26 20:02

Lyrk


1 Answers

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;
...
like image 155
Graham Borland Avatar answered Feb 12 '26 14:02

Graham Borland



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!