Structural confusion

Some time passed when I did not communicate with the C language, so I just went through some of the concepts, but could not find a good source for the structures.

Can anyone explain

struct A { int a; char b; float c; }; 

Is this a declaration or a definition of structure A

+6
source share
2 answers

This is an ad.

struct A; is a forward declaration or an incomplete declaration.

 struct A { int a; char b; float c; }; 

is a complete struct declaration.

Example

Also check comp.lang.c FAQ list Question 11.5

After directly declaring a struct you can use pointers to the structure, but you cannot dereference pointers or use the sizeof operator or create instances of struct .

After the declaration, you can also use struct objects, apply the sizeof operator, etc.


From 6.7.2.1 Structure and Association Qualifiers from C11 Specifications

8 Type is incomplete until immediately after it completes the list and complete after that.

And from 6.7.2.3 Tags

If a form type specifier exists
structure or union identifier except as part of one of the above forms, and no other declaration of the identifier as a tag, then it declares an incomplete structure or union, and declares the identifier as a tag of this type. 131 )
131 A similar listing design does not exist.


This should not be confused with extern struct A aa; v / s struct A aa ={/*Some values*/}; which are the declaration and definitions of the object aa .

+3
source

It declares a structure with a struct A tag and the specified members. It does not define or reserve any storage for the object.

From the standard C99, 6.7 Announcements:

Semantics

5 The description indicates the interpretation and attributes of the set of identifiers. An identifier definition is a declaration for that identifier, which:

- for an object, forces the storage to be reserved for this object;

- for a function, includes the body of the function; (footnote 98)

- for enumeration constant or typedef name, is the (only) declaration Identifier.

For definition, you will need to provide the identifier of the object until the final semicolon:

 struct A { int a; char b; float c; } mystruct; 

To also initialize mystruct, you must write

 struct A { int a; char b; float c; } mystruct = { 42, 'x', 3.14 }; 
+11
source

Source: https://habr.com/ru/post/983093/


All Articles