A struct is a type consisting of a sequence of members whose storage is allocated in an ordered sequence (as opposed to union, which is a type consisting of a sequence of members whose storage overlaps).
The type specifier for a struct is identical to the union
type specifier except for the keyword used:
struct attr-spec-seq(optional) name(optional) { struct-declaration-list } | (1) | |
struct attr-spec-seq(optional) name | (2) |
struct
name ;
, declares but doesn't define the struct name
(see forward declaration below). In other contexts, names the previously-declared struct, and attr-spec-seq is not allowed.name | - | the name of the struct that's being defined |
struct-declaration-list | - | any number of variable declarations, bit field declarations, and static assert declarations. Members of incomplete type and members of function type are not allowed (except for the flexible array member described below) |
attr-spec-seq | - | (C23)optional list of attributes, applied to the struct type |
Within a struct object, addresses of its elements (and the addresses of the bit field allocation units) increase in order in which the members were defined. A pointer to a struct can be cast to a pointer to its first member (or, if the member is a bit field, to its allocation unit). Likewise, a pointer to the first member of a struct can be cast to a pointer to the enclosing struct. There may be unnamed padding between any two members of a struct or after the last member, but not before the first member. The size of a struct is at least as large as the sum of the sizes of its members.
A declaration of the following form.
struct attr-spec-seq(optional) name ; |
hides any previously declared meaning for the name name in the tag name space and declares name as a new struct name in current scope, which will be defined later. Until the definition appears, this struct name has incomplete type.
This allows structs that refer to each other:
Note that a new struct name may also be introduced just by using a struct tag within another declaration, but if a previously declared struct with the same name exists in the tag name space, the tag would refer to that name.
struct s* p = NULL; // tag naming an unknown struct declares it struct s { int a; }; // definition for the struct pointed to by p void g(void) { struct s; // forward declaration of a new, local struct s // this hides global struct s until the end of this block struct s *p; // pointer to local struct s // without the forward declaration above, // this would point at the file-scope s struct s { char* p; }; // definitions of the local struct s }
See struct initialization for the rules regarding the initializers for structs.
Because members of incomplete type are not allowed, and a struct type is not complete until the end of the definition, a struct cannot have a member of its own type. A pointer to its own type is allowed, and is commonly used to implement nodes in linked lists or trees.
Because a struct declaration does not establish scope, nested types, enumerations and enumerators introduced by declarations within struct-declaration-list are visible in the surrounding scope where the struct is defined.
#include <stddef.h> #include <stdio.h> int main(void) { struct car { char *make; char *model; int year; }; // declares the struct type // declares and initializes an object of a previously-declared struct type struct car c = {.year=1923, .make="Nash", .model="48 Sports Touring Car"}; printf("car: %d %s %s\n", c.year, c.make, c.model); // declares a struct type, an object of that type, and a pointer to it struct spaceship { char *make; char *model; char *year; } ship = {"Incom Corporation", "T-65 X-wing starfighter", "128 ABY"}, *pship = &ship; printf("spaceship: %s %s %s\n", ship.year, ship.make, ship.model); // addresses increase in order of definition // padding may be inserted struct A { char a; double b; char c;}; printf("offset of char a = %zu\noffset of double b = %zu\noffset of char c = %zu\n" "sizeof(struct A) = %zu\n", offsetof(struct A, a), offsetof(struct A, b), offsetof(struct A, c), sizeof(struct A)); struct B { char a; char b; double c;}; printf("offset of char a = %zu\noffset of char b = %zu\noffset of double c = %zu\n" "sizeof(struct B) = %zu\n", offsetof(struct B, a), offsetof(struct B, b), offsetof(struct B, c), sizeof(struct B)); // A pointer to a struct can be cast to a pointer to its first member and vice versa char* pmake = (char*)pship; pship = (struct spaceship *)pmake; }
Possible output:
C++ documentation for Class declaration |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/c/language/struct