2

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’

4 Answers 4

6

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.)

Sign up to request clarification or add additional context in comments.

Comments

5
  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.

1 Comment

With gcc -Wall to enable all warnings, in MSVC it's warning level 4 in one of the options
1

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.

1 Comment

There no requirement to "initialize both values".
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.