The syntax for a struct declaration is shown by this simple example:
The tag_name is optional in some contexts.
Via the keyword typedef, a struct type can be referenced without using the struct keyword. However, some[who?] programming style guides advise against this, claiming that it can obfuscate the type.
For example:
In C++ code, typedef is not needed because types defined via struct are part of the regular namespace, so the type can be referred to as either struct thing_t or thing_t.
There are three ways to initialize a structure.
For the type:
C89-style initializers are used when contiguous members may be given.3 For example:
For non contiguous or out of order members list, designated initializer style may be used.4 For example:
If an initializer is given or if the object is statically allocated, omitted elements are initialized to 0.
A third way of initializing a structure is to copy the value of an existing object of the same type. For example:
The state of a struct can be copied to another instance. A compiler might use memcpy() to copy the bytes of the memory block.
Pointers can be used to refer to a struct by its address. This is useful for passing a struct to a function to avoid the overhead of copying the struct. The -> operator dereferences the pointer (left operand) and accesses the value of a struct member (right operand).
In C++, struct is essentially the same as for C. Further, a class is the same as a struct but with different default visibility: class members are private by default, whereas struct members are public by default.
.NET languages have a feature similar to struct in C – called struct in C# and Structure in Visual Basic .NET). This construct provides many features of a class, but acts as a value type instead of a reference type. For example, when passing a .NET struct to a function, the value is copied so that changes to the input parameter do not affect the value passed in.5
"Struct memory layout in C". Stack Overflow. https://stackoverflow.com/questions/2748995/struct-memory-layout-in-c ↩
Ritchie, Dennis M. (March 1993). "The Development of the C Language". ACM SIGPLAN Notices. 28 (3): 201–208. doi:10.1145/155360.155580. The scheme of type composition adopted by C owes considerable debt to Algol 68, although it did not, perhaps, emerge in a form that Algol's adherents would approve of. The central notion I captured from Algol was a type structure based on atomic types (including structures), composed into arrays, pointers (references), and functions (procedures). Algol 68's concept of unions and casts also had an influence that appeared later. /wiki/Dennis_Ritchie ↩
Kelley, Al; Pohl, Ira (2004). A Book On C: Programming in C (Fourth ed.). pp. 418. ISBN 0-201-18399-4. 0-201-18399-4 ↩
"IBM Linux compilers. Initialization of structures and unions". https://www.ibm.com/support/knowledgecenter/en/SSLTBW_2.3.0/com.ibm.zos.v2r3.cbclx01/strin.htm ↩
"Parameter passing in C#". http://yoda.arachsys.com/csharp/parameters.html ↩