cstructwarnings

Warning "declaration does not declare anything"


Consider:

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

int main()
{
    struct emp
    {
        struct address
        {
              int a;
        };
        struct address a1;
    };
}

This code shows a warning:

warning : declaration does not declare anything (enabled by default)

Whereas the following code doesn't show any warning:

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

int main()
{
    struct emp
    {
        struct address
        {
             int a;
        } a1;
    };
}

Why is 'warning' displayed in the first code only?


Solution

  • The compiler is showing the warning, because it doesn't see a name for the variable of type address you defined for the emp struct, even though you do declare something using address on the next line, but I guess the compiler is not smart enough to figure that out.

    As you showed, this produces a warning:

    struct emp {
        struct address {}; // This statement doesn't declare any
                           // variable for the 'emp' struct.
        struct address a1;
    };
    

    But not this:

    struct emp {
        struct address {} a1; // This statement defines the address
                              // struct and the a1 variable.
    };
    

    Or this:

    struct address {};
    
    struct emp {
        struct address a1; // The only statement declare a variable
                           // of type struct address
    };
    

    The struct emp {} doesn't show any warnings since this statement is not inside a struct definition block. If you did put it inside one of those, then the compiler would have shown a warning for that as well. The following will show two warnings:

    struct emp {
        struct phone {};
        struct name {};
    };