Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
C syntax
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
====Declaration==== Structures are declared with the [[struct (C programming language)|{{code|struct}}]] keyword and unions are declared with the {{code|union}} keyword. The specifier keyword is followed by an optional identifier name, which is used to identify the form of the structure or union. The identifier is followed by the declaration of the structure or union's body: a list of member declarations, contained within curly braces, with each declaration terminated by a semicolon. Finally, the declaration concludes with an optional list of identifier names, which are declared as instances of the structure or union. For example, the following statement declares a structure named {{code|s}} that contains three members; it will also declare an instance of the structure known as {{code|tee}}: <syntaxhighlight lang=C> struct s { int x; float y; char *z; } tee; </syntaxhighlight> And the following statement will declare a similar union named {{code|u}} and an instance of it named {{code|n}}: <syntaxhighlight lang=C> union u { int x; float y; char *z; } n; </syntaxhighlight> Members of structures and unions cannot have an incomplete or function type. Thus members cannot be an instance of the structure or union being declared (because it is incomplete at that point) but can be pointers to the type being declared. Once a structure or union body has been declared and given a name, it can be considered a new data type using the specifier {{code|struct}} or {{code|union}}, as appropriate, and the name. For example, the following statement, given the above structure declaration, declares a new instance of the structure {{code|s}} named {{code|r}}: <syntaxhighlight lang=C>struct s r;</syntaxhighlight> It is also common to use the <code>[[typedef]]</code> specifier to eliminate the need for the {{code|struct}} or {{code|union}} keyword in later references to the structure. The first identifier after the body of the structure is taken as the new name for the structure type (structure instances may not be declared in this context). For example, the following statement will declare a new type known as ''s_type'' that will contain some structure: <syntaxhighlight lang=C>typedef struct {...} s_type;</syntaxhighlight> Future statements can then use the specifier ''s_type'' (instead of the expanded {{code|struct}} ... specifier) to refer to the structure.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)