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
Struct (C programming language)
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!
{{Short description|C keyword for defining a structured data type}} {{lowercase}} In the [[C programming language]], '''struct''' is the keyword used to define a [[composite data type|composite]], a.k.a. [[Record (computer science)|record]], [[data type]] {{endash}} a named set of values that occupy a block of memory. It allows for the different values to be accessed via a single [[identifier]], often a [[pointer (computer programming)|pointer]]. A struct can contain other data types so is used for mixed-data-type records. For example a bank customer struct might contains fields: name, address, telephone, balance. A struct occupies a ''contiguous block'' of memory, usually delimited (sized) by word-length boundaries. It corresponds to the similarly named feature available in some [[assembly language|assemblers]] for Intel processors. Being a block of contiguous memory, each field within a struct is located at a certain fixed offset from the start. The [[sizeof]] operator results in the number of bytes needed to store a particular struct, just as it does for a [[primitive data type]]. The alignment of particular fields in the struct (with respect to [[Word (computer architecture)|word]] boundaries) is implementation-specific and may include padding. Modern compilers typically support the <code>#pragma pack</code> directive, which sets the size in bytes for alignment.<ref>{{Cite web|url=https://stackoverflow.com/questions/2748995/struct-memory-layout-in-c|title=Struct memory layout in C|website=Stack Overflow}}</ref> The C struct feature was derived from the same-named concept in [[ALGOL 68]].<ref name="sigplan">{{cite journal | first = Dennis M.| last = Ritchie | authorlink = Dennis Ritchie | title = The Development of the C Language | date = March 1993 | journal = ACM SIGPLAN Notices | volume = 28 | issue = 3 | pages = 201β208 | url = http://www.bell-labs.com/usr/dmr/www/chist.html | doi = 10.1145/155360.155580 | quote = 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.| doi-access = free }}</ref> == Declaration == The syntax for a struct declaration is shown by this simple example: <syntaxhighlight lang="C"> struct tag_name { type member1; type member2; }; </syntaxhighlight> The <code>tag_name</code> is optional in some contexts. ==Typedef== Via the keyword [[Typedef|<code>typedef</code>]], a struct type can be referenced without using the <code>struct</code> keyword. However, some{{who|date=October 2021}} programming style guides advise against this, claiming that it can obfuscate the type. For example: <syntaxhighlight lang="C"> typedef struct tag_name { type member1; type member2; } thing_t; thing_t thing; </syntaxhighlight> In C++ code, typedef is not needed because types defined via <code>struct</code> are part of the regular namespace, so the type can be referred to as either <code>struct thing_t</code> or <code>thing_t</code>. == Initialization == There are three ways to initialize a structure. For the type: <syntaxhighlight lang="c"> struct point_t { int x; int y; }; </syntaxhighlight> ''C89-style initializers'' are used when contiguous members may be given.<ref>{{cite book | first1 = Al | last1 = Kelley | first2 = Ira | last2 = Pohl | year = 2004 | url = https://archive.org/details/bookoncprogrammi00kell/page/418 | edition = Fourth | title = A Book On C: Programming in C | isbn = 0-201-18399-4 | pages = [https://archive.org/details/bookoncprogrammi00kell/page/418 418] | url-access = registration }}</ref> For example: <syntaxhighlight lang="c"> struct point_t a = { 1, 2 }; </syntaxhighlight> For non contiguous or out of order members list, ''designated initializer'' style may be used.<ref>{{cite web | url=https://www.ibm.com/support/knowledgecenter/en/SSLTBW_2.3.0/com.ibm.zos.v2r3.cbclx01/strin.htm | title=IBM Linux compilers. Initialization of structures and unions}}</ref> For example: <syntaxhighlight lang="c"> struct point_t a = { .y = 2, .x = 1 }; </syntaxhighlight> If an initializer is given or if the object is [[Static memory allocation|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: <syntaxhighlight lang="c"> struct point_t b = a; </syntaxhighlight> == Copy == The state of a struct can be copied to another instance. A compiler might use <code>memcpy()</code> to copy the bytes of the memory block. <syntaxhighlight lang="c"> struct point_t a = { 1, 3 }; struct point_t b; b = a; </syntaxhighlight> == Pointers == Pointers can be used to refer to a <code>struct</code> by its address. This is useful for passing a struct to a function to avoid the overhead of copying the struct. The <code>-></code> operator [[Dereference operator|dereferences]] the pointer (left operand) and accesses the value of a struct member (right operand). <syntaxhighlight lang="c"> struct point_t point = { 3, 7 }; int x = point.x; point.x = 10; struct point_t *pp = &point; x = pp->x; pp->x = 8; </syntaxhighlight> == In other languages == ===C++=== In [[C++]], struct is essentially the same as for C. Further, a [[C++ class|class]] is the same as a struct but with different default [[Access modifiers|visibility]]: class members are private by default, whereas struct members are public by default. ===.NET=== [[.NET]] languages have a feature similar to struct in C {{endash}} called <code>struct</code> in [[C Sharp (programming language)|C#]] and <code>Structure</code> 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.<ref>{{Cite web|url=http://yoda.arachsys.com/csharp/parameters.html|title=Parameter passing in C#}}</ref> == See also == * {{Annotated link|Bit field}} * {{Annotated link|Flexible array member}} * {{Annotated link|Passive data structure}} * {{Annotated link|Union type}} == References == {{reflist}} [[Category:C (programming language)]]
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)
Pages transcluded onto the current version of this page
(
help
)
:
Template:Annotated link
(
edit
)
Template:Cite book
(
edit
)
Template:Cite journal
(
edit
)
Template:Cite web
(
edit
)
Template:Endash
(
edit
)
Template:Lowercase
(
edit
)
Template:Reflist
(
edit
)
Template:Short description
(
edit
)
Template:Who
(
edit
)