TSG
TSG

Reputation: 4617

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)

Upvotes: 0

Views: 163

Answers (2)

user1995314
user1995314

Reputation:

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}};

Upvotes: 3

ouah
ouah

Reputation: 145829

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

This is perfectly valid C, the warning is just an indication from your compiler in this case.

If you want to get rid of the warning you can do:

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

or also add -Wno-missing-braces if you are using gcc with -Wall options.

Upvotes: 3

Related Questions