Can I use a structure tag in front of its scope?

In the following example:

#include <stdio.h> #include <stdlib.h> struct B b; struct B{int a;}; struct B; int main() { ba=3; printf("%d\n", ba); return 0; } 

The file size of the structure tag B begins with its first definition / declaration struct B{int a;}; . Like struct B b; to reference B to region B without errors?

Is struct B; a structure type declaration or a structure type definition? Why does it not conflict with another definition struct B{int a;}; in the same file scope?

+2
c struct
source share
2 answers

struct B; is a structure declaration. There can be any number of declarations of the same object. For object types that have several possible declarations, declarations must be compatible - for example, you can have several prototypes for the same function, but they need to use the same arguments and return data types. For a structure, there is only one way to declare it without defining it, and it simply says "there is a structure called B ". So it's ok to repeat struct B; as many times as you want.

struct B{int a;}; is a definition of structure. There can be only one definition of a given object. Some uses of the property require a preliminary definition, others require only a preliminary announcement. For example, you can define a variable whose type is a pointer to an undefined structure:

 struct S; struct S *p = NULL; 

But you cannot define a variable whose type is an undefined structure:

 struct S; struct S s = {0}; 

The reason for this difference is that a structure that is not yet defined is an incomplete type β€” a type for which the compiler has no size. This is not a problem for defining a pointer, but the problem is defining an object of an incomplete type: the compiler does not know how much memory is required.

At this point, you may wonder why struct B b; OK. In the end, struct B was not even declared, much less defined.

The first part, why this is so, means that struct B b; passes structure B along the way. Therefore, by the time struct B needs to do something with B , the structure has just been declared.

The second part, why is it normal that struct B b; not an exact definition. If this were a definition, then the compiler would have to know how much space to reserve, and the incomplete type is not in order. This is what happens if the variable was defined in the function:

 void f(void) { struct B b; // error: storage size of 'b' isn't known } 

But when struct B b; located at the top level of the file, this is a preliminary definition. A preliminary definition is an announcement; for this, the structure should not be defined. When the compiler gets to the end of the file, any definition that does not have a correct definition and that requires a definition becomes a definition. At this stage, the structure should be defined. In your sample program, B has a preliminary definition, and that definition becomes the correct definition at the end of the file.

It is normal to define a structure if a variable is not used, because it is normal to declare a variable with an incomplete type if the variable is not used.

 struct B b; // end of file 

But if a variable is used in a way that requires a full type, then the structure must be defined at this point.

 struct B b; void f(void) { sizeof(b); // error: invalid application of 'sizeof' to incomplete type 'struct B' } struct B{int a;}; // too late 
+5
source share

Question: How can structure B b; to reference B to region B without errors? Answer: From http://c0x.coding-guidelines.com/6.7.2.3.html in section 1471, "If a type specifier in the form of a struct-or-union identifier occurs differently than as part of one of the above forms, and no other the declaration of the identifier as a tag is not visible, then it declares an incomplete structure or type of union and declares the identifier as a tag of this type. "
Thus, you can use the struct tag as a direct declaration before defining the type of structure.

Question: Is structure B; structure type declaration or structure type definition? Why it does not contradict another definition struct B {int a;}; in the same file size? Answer: From the same link in section 1472, β€œIf a specifier of the type of a structure identifier or a union or an enumeration identifier occurs differently than as part of one of the above forms, and the identifier declaration in the form of a tag is visible, then it indicates the same type as the other ad and doesn’t update the tag. "

struct B; is a structure type declaration, struct B {int a;}; it is a definition of the type of structure and how they do not conflict with each other. You can use typedef to get rid of the confusion and show more visibility in the declaration of your structure:

 typedef struct BB; struct B{ int a; }; B b; 
+1
source share

All Articles