Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8785207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:17:41+00:00 2026-06-13T21:17:41+00:00

I want to initialize all struct members to 0. Common solution is to make

  • 0

I want to initialize all struct members to 0. Common solution is to make something like this:

struct foo bar = {0}

I create this example:

#include <stdio.h>

struct Stru2 {
    int c;
    int d;
};

struct Stru1 {
    int a;
    Stru2 b;
};

int main()
{
    struct Stru1 aaa = { 0 };
    return aaa.b.c;
}

And I compile (gcc 4.6.3) it whit this parameters, to make sure how ANSI handle this

gcc -Wall -Wextra -pedantic -ansi main.cpp

And I got following warnings:

main.cpp: In function ‘int main()’: 
main.cpp:36:28: warning: missing initializer for member ‘Stru1::b’ [-Wmissing-field-initializers]

The question is, why -Wextra, generate this warning? Maybe not always “= {0}”, set all members to 0?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T21:17:43+00:00Added an answer on June 13, 2026 at 9:17 pm

    It’s just a nanny warning.. = {0} definitely sets all of the members to zero. The reason it’s included as a warning at all is for the case where you might add a field to a structure and you want to make sure it gets initialized correctly. Having the warning lets you find those places quickly and fix them. For example, let’s say you fix have an initializer something like:

    struct Stru1 aaa = { 1, { 2, 3 } };
    

    Now, add a field int e to Stru2:

    struct Stru2 {
        int c;
        int d;
        int e;
    };
    

    And let’s pretend it’s very important that e get initialized. Well, without -Wmissing-field-initializers, you’d have to hunt around in your code to find every place you care about. With the warning, you get a diagnostic message like the one you saw, and you can find and fix them very easily:

    struct Stru1 aaa = { 1, { 2, 3, 4 } };
    

    To get the advantage of this warning, you do have to go through and fully initialize all of your structures, though. In your case, that means something like:

    struct Stru1 aaa = { 0, { 0, 0 } };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to initialize all elements of: char buffer[1000][1000]; to ' ' . I've
I want to create an environment class that is accessible from all of my
I want to convert this C++ code to C#: typedef struct consoleCommand_s { char*
can someone explain me the compilation error in this code: #include common.h typedef struct
I have a C library that has types like this: typedef struct { //
I have this structure: typedef struct { char *str; ... } nameType; I create
I want to use a function to allocate and initialize two related struct instances.
I have vector< pair<int, int>> myVec (N); I want to have all pairs initialized
I want to initialize a static Class variable in Java: public class NumberExpression {
I want to initialize a couple of variables on Excel Dna .dll gets loaded

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.