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)
(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!
== 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>
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)