Declaration declares nothing: warning?

#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: ad declares nothing (enabled by default)

If the following code does not display a warning

 #include <stdio.h> #include <stdlib.h> #include <conio.h> int main() { struct emp { struct address { int a; }a1; }; } 

Why is a "warning" displayed only in the first code?

+11
c struct warnings
source share
3 answers

The reason the compiler shows a warning is because it does not see the name of the variable of type address that you defined for the emp structure, even if you do declare something using address on the next line, but I think the compiler is not smart enough to understand this.

As you have shown, this causes 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 that:

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

struct emp {} does not display any warnings, because this statement is not inside the structure detection block. If you put it inside one of them, then the compiler will also show a warning. The following are two warnings:

 struct emp { struct phone {}; struct name {}; }; 
+9
source share

Structure Definition Syntax:

 struct identifier { type member_name; // ... }; 

If you add an identifier immediately after closing the curly brace, you declare a variable with the specified structure.

In your first example, the compiler treats the address structure as a member type. like this if you write:

 struct identifier { type ; // No member name is specified type a1; // ... } 

But in the second example, you specified the name of the member:

 struct identifier { type a1; // Member name specified // ... } 

And here is a warning example: http://ideone.com/KrnYiE .

+3
source share

The reason the warning is displayed is because the first passage is not correct. C - it has a restriction restriction that a compiler of C that complies with the standards must issue a diagnostic message. It violates C11 6.7.2.1p2 :

Limitations

  1. A structure declaration that does not declare an anonymous structure or an anonymous union must contain a list of struct-declarators.

It means it's ok to write

 struct foo { struct { int a; }; }; 

since the inner struct declares an anonymous structure, i.e. it is not called.

But in your example, the struct address has the name - address - and, therefore, it should have a list of declarators after the list of closing lists of figures, for example, a1 as in your example, or the more complex foo, *bar, **baz[23][45] .

+1
source share

All Articles