<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.sarg.dev/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=2600%3A4040%3A7EFD%3A5700%3A54CB%3A6027%3A3DA2%3A8B3B</id>
	<title>Vero - Wikipedia - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.sarg.dev/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=2600%3A4040%3A7EFD%3A5700%3A54CB%3A6027%3A3DA2%3A8B3B"/>
	<link rel="alternate" type="text/html" href="https://wiki.sarg.dev/index.php/Special:Contributions/2600:4040:7EFD:5700:54CB:6027:3DA2:8B3B"/>
	<updated>2026-08-14T19:45:45Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.44.2</generator>
	<entry>
		<id>https://wiki.sarg.dev/index.php?title=Struct_(C_programming_language)&amp;diff=586170</id>
		<title>Struct (C programming language)</title>
		<link rel="alternate" type="text/html" href="https://wiki.sarg.dev/index.php?title=Struct_(C_programming_language)&amp;diff=586170"/>
		<updated>2025-10-22T14:41:16Z</updated>

		<summary type="html">&lt;p&gt;2600:4040:7EFD:5700:54CB:6027:3DA2:8B3B: fix grammar&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Short description|C keyword for defining a structured data type}}&lt;br /&gt;
{{lowercase}}&lt;br /&gt;
In the [[C programming language]], &#039;&#039;&#039;struct&#039;&#039;&#039; 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 contain fields for the customer&#039;s name, address, telephone number, and balance.&lt;br /&gt;
&lt;br /&gt;
A struct occupies a &#039;&#039;contiguous block&#039;&#039; 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.&lt;br /&gt;
&lt;br /&gt;
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 &amp;lt;code&amp;gt;#pragma pack&amp;lt;/code&amp;gt; directive, which sets the size in bytes for alignment.&amp;lt;ref&amp;gt;{{Cite web|url=https://stackoverflow.com/questions/2748995/struct-memory-layout-in-c|title=Struct memory layout in C|website=Stack Overflow}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The C struct feature was derived from the same-named concept in [[ALGOL 68]].&amp;lt;ref name=&amp;quot;sigplan&amp;quot;&amp;gt;{{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&#039;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&#039;s concept of unions and casts also had an influence that appeared later.| doi-access = free }}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Declaration ==&lt;br /&gt;
&lt;br /&gt;
The syntax for a struct declaration is shown by this simple example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
struct TagName {&lt;br /&gt;
    Type member1;&lt;br /&gt;
    Type member2;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;TagName&amp;lt;/code&amp;gt; is optional in some contexts.&lt;br /&gt;
&lt;br /&gt;
==Typedef==&lt;br /&gt;
&lt;br /&gt;
Via the keyword [[Typedef|&amp;lt;code&amp;gt;typedef&amp;lt;/code&amp;gt;]], a struct type can be referenced without using the &amp;lt;code&amp;gt;struct&amp;lt;/code&amp;gt; keyword. However, some&amp;lt;ref&amp;gt;{{Cite web|url=https://www.kernel.org/doc/html/v4.10/process/coding-style.html#typedefs|title=Linux kernel coding style}}&amp;lt;/ref&amp;gt; programming style guides advise against this, claiming that it can obfuscate the type.&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
typedef struct TagName {&lt;br /&gt;
   Type member1;&lt;br /&gt;
   Type member2;&lt;br /&gt;
} Thing;&lt;br /&gt;
&lt;br /&gt;
// struct TagName can now be referred to as Thing&lt;br /&gt;
Thing thing;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In C++ code, &amp;lt;code&amp;gt;typedef&amp;lt;/code&amp;gt; is not needed because types defined via &amp;lt;code&amp;gt;struct&amp;lt;/code&amp;gt; are part of the regular namespace, so the type can be referred to as either &amp;lt;code&amp;gt;struct Thing&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;Thing&amp;lt;/code&amp;gt;. &amp;lt;code&amp;gt;typedef&amp;lt;/code&amp;gt; in C++ is also superseded by the &amp;lt;code&amp;gt;using&amp;lt;/code&amp;gt; statement, which can alias types that have [[template (C++)|templates]].&lt;br /&gt;
&lt;br /&gt;
== Initialization ==&lt;br /&gt;
&lt;br /&gt;
There are three ways to initialize a structure. &lt;br /&gt;
&lt;br /&gt;
For the type:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
struct Point {&lt;br /&gt;
    int x;&lt;br /&gt;
    int y;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;C89-style initializers&#039;&#039; are used when contiguous members may be given.&amp;lt;ref&amp;gt;{{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 }}&amp;lt;/ref&amp;gt; For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
struct Point a = { 1, 2 };&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For non contiguous or out of order members list, &#039;&#039;designated initializer&#039;&#039; style may be used.&amp;lt;ref&amp;gt;{{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}}&amp;lt;/ref&amp;gt; For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
struct Point a = { .y = 2, .x = 1 };&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If an initializer is given or if the object is [[Static memory allocation|statically allocated]], omitted elements are initialized to 0.&lt;br /&gt;
&lt;br /&gt;
A third way of initializing a structure is to copy the value of an existing object of the same type. For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
struct Point b = a;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Copy ==&lt;br /&gt;
&lt;br /&gt;
The state of a struct can be copied to another instance. A compiler might use &amp;lt;code&amp;gt;memcpy()&amp;lt;/code&amp;gt; to copy the bytes of the memory block.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
struct Point a = { 1, 3 };&lt;br /&gt;
struct Point b;&lt;br /&gt;
b = a;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Pointers ==&lt;br /&gt;
&lt;br /&gt;
Pointers can be used to refer to a &amp;lt;code&amp;gt;struct&amp;lt;/code&amp;gt; by its address. This is useful for passing a struct to a function to avoid the overhead of copying the struct. The &amp;lt;code&amp;gt;-&amp;gt;&amp;lt;/code&amp;gt; operator [[Dereference operator|dereferences]] the pointer (left operand) and accesses the value of a struct member (right operand).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
struct Point p = { 3, 7 };&lt;br /&gt;
int x = p.x;&lt;br /&gt;
p.x = 10;&lt;br /&gt;
struct Point* pp = &amp;amp;p;&lt;br /&gt;
x = pp-&amp;gt;x;&lt;br /&gt;
pp-&amp;gt;x = 8;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== In other languages ==&lt;br /&gt;
[[D (programming language)|D]],&amp;lt;ref&amp;gt;{{cite web |title=Structs, Unions - D Programming Language |url=https://dlang.org/spec/struct.html |website=dlang.org |access-date=8 July 2025}}&amp;lt;/ref&amp;gt; [[Go (programming language)|Go]], [[Julia (programming language)|Julia]],&amp;lt;ref&amp;gt;{{cite web |title=Types · The Julia Language |url=https://docs.julialang.org/en/v1/manual/types/#Composite-Types |website=docs.julialang.org |language=en}}&amp;lt;/ref&amp;gt; [[Rust (programming language)|Rust]], [[Swift (programming language)|Swift]] and [[Zig (programming language)|Zig]]&amp;lt;ref&amp;gt;{{cite web |title=Structs {{!}} zig.guide |url=https://zig.guide/language-basics/structs/ |website=zig.guide |access-date=8 July 2025 |language=en |date=20 April 2024}}&amp;lt;/ref&amp;gt; have structs.&lt;br /&gt;
&lt;br /&gt;
===C++===&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
===.NET===&lt;br /&gt;
&lt;br /&gt;
[[.NET]] languages have a feature similar to struct in C {{endash}} called &amp;lt;code&amp;gt;struct&amp;lt;/code&amp;gt; in [[C Sharp (programming language)|C#]] and &amp;lt;code&amp;gt;Structure&amp;lt;/code&amp;gt; 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.&amp;lt;ref&amp;gt;{{Cite web|url=http://yoda.arachsys.com/csharp/parameters.html|title=Parameter passing in C#}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
* {{Annotated link|Bit field}}&lt;br /&gt;
* {{Annotated link|Flexible array member}}&lt;br /&gt;
* {{Annotated link|Passive data structure}}&lt;br /&gt;
* {{Annotated link|Union type}}&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
{{reflist}}&lt;br /&gt;
&lt;br /&gt;
[[Category:C (programming language)]]&lt;/div&gt;</summary>
		<author><name>2600:4040:7EFD:5700:54CB:6027:3DA2:8B3B</name></author>
	</entry>
</feed>