tags:

views:

88

answers:

4

Are these 2 methods equivalent ?

char a[10] = "";
char a[10] = { 0 };

Also, I would like to declare-initialize a struct but this will not work in gcc:

struct c
{
    char d[10];
    int e; 
};
struct c f = { 0 };

a.c: In function ‘main’:
a.c:33: warning: missing braces around initializer
a.c:33: warning: (near initialization for ‘f.d’)
a.c:33: warning: missing initializer
a.c:33: warning: (near initialization for ‘f.e’)
a.c:33: warning: unused variable ‘f’

+1  A: 

1) Yes they are equivalent. 2) Try

struct c f = { "", 0 }

You must initialize both value. I remember having issues too, you can try

struct c f = (struct c){ "", 0 }

if it doesn't work.

See this page too.

Alexandre C.
There no requirement to "initialize both values".
AndreyT
+6  A: 

Yes, the two char[] initialisations are identical - both will initialise the array to contain zeros.

For the structure problem, try:

struct c f = { {0}, 0 };

(With newer versions of gcc that support C99 you can also name the elements of the structure being initialised.)

psmears
+3  A: 
  1. Yes, they are equivalent.

  2. Under default options, the structure initialization compiles without warning. If you set the compiler to fussy, then you need to provide a string (either of the two forms in your first question) and an integer:

    struct c
    {
        char d[10];
        int e; 
    };
    struct c f = { "", 0 };
    struct c g = { { 0 }, 0 };
    

What does 'fussy' mean:

Osiris-2 JL: cat x.c
struct c
{
    char d[10];
    int e; 
};
struct c f = { 0 };
Osiris-2 JL: gcc -c x.c
Osiris-2 JL: gcc -Wall -c x.c
x.c:6: warning: missing braces around initializer
x.c:6: warning: (near initialization for ‘f.d’)
Osiris-2 JL: gcc -O -Wall -c x.c
x.c:6: warning: missing braces around initializer
x.c:6: warning: (near initialization for ‘f.d’)
Osiris-2 JL: gcc -O -Wextra -Wall -c x.c
x.c:6: warning: missing braces around initializer
x.c:6: warning: (near initialization for ‘f.d’)
x.c:6: warning: missing initializer
x.c:6: warning: (near initialization for ‘f.e’)
Osiris-2 JL: 

In this context, GCC 'set fussy' means adding options like '-Wall' and '-Wextra' to get more warnings than are required by the C standard that GCC is compiling to. Since I didn't specify which standard, it is working to the GNU-99 standard (-std=gnu99).

To get the 'unused variable' message from the question, I would have to make the variable f into a static variable.

Jonathan Leffler
What do you mean with fussy?
Job
With gcc -Wall to enable all warnings, in MSVC it's warning level 4 in one of the options
Martin Beckett
+1  A: 

Yes, the first two initialization are equivalent.

The second initialization will work in GCC. Your code is perfectly correct. In C language the { 0 } is an Universal Zero Initializer usable with virtually any type. This is an old and very well established idiom. What you get from GCC is just warnings, put there by someone who made a rather clueless decision to interfere with the functionality of the Universal Zero Initializer.

[rant]GCC has been going down the drain lately in some respects.[/rant] I understand their general intent, but the specific { 0 } form should have been handled as a warning-less exception.

AndreyT