Search code examples
arrayscinitializationansi-c

Ansi C complains about array initializing


I have a simple C program that compiles fine under c99, but under ANSI it complains:

missing braces around initializer

The offending line is:

int myarr[3][3]={0};

Why is ANSI C complaining? I saw one posting saying to add additional { } around the { 0 } but that makes no sense to me...

(I'm compiling in CentOS in case it matters)


Solution

  • Strictly (under ANSI C) you should additional curly braces if you were to initialising a multi-dimensional array. For example if one initialises each element to a specific value one would do the following:

    int myarr[3][3] = {{1,2,3},{4,5,6},{7,8,9}};