Knowledge (XXG)

struct (C programming language)

Source 📝

761:
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,
47:. 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. 668:. 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. 58:
for Intel processors. Being a block of contiguous memory, each field within a struct is located at a certain fixed offset from the start.
841: 791: 762:
pointers (references), and functions (procedures). Algol 68's concept of unions and casts also had an influence that appeared later.
807: 54:
of memory, usually delimited (sized) by word-length boundaries. It corresponds to the similarly named feature available in some
649: 44: 70: 39:– a named set of values that occupy a block of memory. It allows for the different values to be accessed via a single 470:
by its address. This is useful for passing a struct to a function to avoid the overhead of copying the struct. The
369:
A third way of initializing a structure is to copy the value of an existing object of the same type. For example:
32: 363: 151:
keyword. However, some programming style guides advise against this, claiming that it can obfuscate the type.
686: – C language feature in which a struct may contain as its last member an array with no specified size 689: 20: 716: 683: 73:
boundaries) is implementation-specific and may include padding. Modern compilers typically support the
475: 66: 65:
operator results in the number of bytes needed to store a particular struct, just as it does for a
28: 787: 783: 776: 657: 55: 750: 630: 734: 665: 698: – Data type that allows for values that are one of multiple different data types 633:: class members are private by default, whereas struct members are public by default. 478:
the pointer (left operand) and accesses the value of a struct member (right operand).
835: 821: 738: 626: 695: 661: 399:
The state of a struct can be copied to another instance. A compiler might use
40: 677: 213:
are part of the regular namespace, so the type can be referred to as either
36: 755: 81: 142: 92:
The syntax for a struct declaration is shown by this simple example:
62: 69:. The alignment of particular fields in the struct (with respect to 660:). This construct provides many features of a class, but acts as a 622: 641: 80:
The C struct feature was derived from the same-named concept in
808:"IBM Linux compilers. Initialization of structures and unions" 209:
In C++ code, typedef is not needed because types defined via
272:
are used when contiguous members may be given. For example:
680: – Data structure that maps one or more adjacent bits 644:
languages have a feature similar to struct in C – called
77:
directive, which sets the size in bytes for alignment.
625:, struct is essentially the same as for C. Further, a 147:, a struct type can be referenced without using the 629:is the same as a struct but with different default 775: 306:For non contiguous or out of order members list, 229:There are three ways to initialize a structure. 362:If an initializer is given or if the object is 16:C keyword for defining a structured data type 8: 754: 366:, omitted elements are initialized to 0. 708: 403:to copy the bytes of the memory block. 7: 739:"The Development of the C Language" 466:Pointers can be used to refer to a 14: 310:style may be used. For example: 27:is the keyword used to define a 692: – Another term for record 774:Kelley, Al; Pohl, Ira (2004). 133:is optional in some contexts. 1: 778:A Book On C: Programming in C 782:(Fourth ed.). pp.  717:"Struct memory layout in C" 858: 822:"Parameter passing in C#" 842:C (programming language) 480: 405: 371: 312: 274: 234: 156: 94: 690:Passive data structure 308:designated initializer 270:C89-style initializers 21:C programming language 756:10.1145/155360.155580 684:Flexible array member 364:statically allocated 50:A struct occupies a 743:ACM SIGPLAN Notices 67:primitive data type 735:Ritchie, Dennis M. 612:In other languages 658:Visual Basic .NET 849: 826: 825: 818: 812: 811: 804: 798: 797: 781: 771: 765: 764: 758: 731: 725: 724: 713: 655: 647: 607: 604: 601: 598: 595: 592: 589: 586: 583: 580: 577: 574: 571: 568: 565: 562: 559: 556: 553: 550: 547: 544: 541: 538: 535: 532: 529: 526: 523: 520: 517: 514: 511: 508: 505: 502: 499: 496: 493: 490: 487: 484: 473: 469: 457: 454: 451: 448: 445: 442: 439: 436: 433: 430: 427: 424: 421: 418: 415: 412: 409: 402: 390: 387: 384: 381: 378: 375: 358: 355: 352: 349: 346: 343: 340: 337: 334: 331: 328: 325: 322: 319: 316: 302: 299: 296: 293: 290: 287: 284: 281: 278: 265: 262: 259: 256: 253: 250: 247: 244: 241: 238: 220: 216: 212: 205: 202: 199: 196: 193: 190: 187: 184: 181: 178: 175: 172: 169: 166: 163: 160: 150: 145: 141:Via the keyword 132: 125: 122: 119: 116: 113: 110: 107: 104: 101: 98: 76: 52:contiguous block 857: 856: 852: 851: 850: 848: 847: 846: 832: 831: 830: 829: 820: 819: 815: 806: 805: 801: 794: 773: 772: 768: 733: 732: 728: 715: 714: 710: 705: 674: 653: 645: 639: 619: 614: 609: 608: 605: 602: 599: 596: 593: 590: 587: 584: 581: 578: 575: 572: 569: 566: 563: 560: 557: 554: 551: 548: 545: 542: 539: 536: 533: 530: 527: 524: 521: 518: 515: 512: 509: 506: 503: 500: 497: 494: 491: 488: 485: 482: 471: 467: 464: 459: 458: 455: 452: 449: 446: 443: 440: 437: 434: 431: 428: 425: 422: 419: 416: 413: 410: 407: 400: 397: 392: 391: 388: 385: 382: 379: 376: 373: 360: 359: 356: 353: 350: 347: 344: 341: 338: 335: 332: 329: 326: 323: 320: 317: 314: 304: 303: 300: 297: 294: 291: 288: 285: 282: 279: 276: 267: 266: 263: 260: 257: 254: 251: 248: 245: 242: 239: 236: 227: 218: 214: 210: 207: 206: 203: 200: 197: 194: 191: 188: 185: 182: 179: 176: 173: 170: 167: 164: 161: 158: 148: 143: 139: 130: 127: 126: 123: 120: 117: 114: 111: 108: 105: 102: 99: 96: 90: 74: 17: 12: 11: 5: 855: 853: 845: 844: 834: 833: 828: 827: 813: 799: 792: 766: 749:(3): 201–208. 737:(March 1993). 726: 721:Stack Overflow 707: 706: 704: 701: 700: 699: 693: 687: 681: 673: 670: 666:reference type 638: 635: 618: 615: 613: 610: 481: 463: 460: 406: 396: 393: 372: 313: 275: 235: 232:For the type: 226: 225:Initialization 223: 215:struct thing_t 157: 138: 135: 95: 89: 86: 15: 13: 10: 9: 6: 4: 3: 2: 854: 843: 840: 839: 837: 823: 817: 814: 809: 803: 800: 795: 793:0-201-18399-4 789: 785: 780: 779: 770: 767: 763: 757: 752: 748: 744: 740: 736: 730: 727: 722: 718: 712: 709: 702: 697: 694: 691: 688: 685: 682: 679: 676: 675: 671: 669: 667: 664:instead of a 663: 659: 651: 643: 636: 634: 632: 628: 624: 616: 611: 479: 477: 461: 404: 394: 370: 367: 365: 311: 309: 273: 271: 233: 230: 224: 222: 155: 154:For example: 152: 146: 136: 134: 93: 87: 85: 83: 78: 72: 68: 64: 59: 57: 53: 48: 46: 42: 38: 34: 30: 26: 22: 816: 802: 777: 769: 760: 746: 742: 729: 720: 711: 640: 620: 476:dereferences 465: 398: 368: 361: 307: 305: 269: 268: 231: 228: 208: 153: 140: 128: 91: 79: 75:#pragma pack 60: 51: 49: 24: 18: 88:Declaration 703:References 696:Union type 662:value type 631:visibility 56:assemblers 43:, often a 41:identifier 678:Bit field 654:Structure 474:operator 37:data type 31:, a.k.a. 29:composite 836:Category 672:See also 462:Pointers 401:memcpy() 165:tag_name 131:tag_name 100:tag_name 82:ALGOL 68 552:point_t 486:point_t 438:point_t 411:point_t 377:point_t 318:point_t 280:point_t 240:point_t 219:thing_t 198:thing_t 192:thing_t 183:member2 174:member1 159:typedef 144:typedef 137:Typedef 118:member2 109:member1 45:pointer 19:In the 790:  646:struct 549:struct 483:struct 468:struct 435:struct 408:struct 374:struct 315:struct 277:struct 237:struct 211:struct 162:struct 149:struct 97:struct 63:sizeof 33:record 25:struct 627:class 594:-> 582:-> 567:point 564:& 531:point 519:point 489:point 472:-> 201:thing 788:ISBN 652:and 642:.NET 637:.NET 395:Copy 180:type 171:type 129:The 115:type 106:type 71:word 61:The 784:418 751:doi 656:in 648:in 623:C++ 621:In 617:C++ 510:int 255:int 246:int 217:or 838:: 786:. 759:. 747:28 745:. 741:. 719:. 650:C# 591:pp 579:pp 558:pp 543:10 507:}; 432:}; 357:}; 301:}; 264:}; 221:. 124:}; 84:. 35:, 23:, 824:. 810:. 796:. 753:: 723:. 606:; 603:8 600:= 597:x 588:; 585:x 576:= 573:x 570:; 561:= 555:* 546:; 540:= 537:x 534:. 528:; 525:x 522:. 516:= 513:x 504:7 501:, 498:3 495:{ 492:= 456:; 453:a 450:= 447:b 444:; 441:b 429:3 426:, 423:1 420:{ 417:= 414:a 389:; 386:a 383:= 380:b 354:1 351:= 348:x 345:. 342:, 339:2 336:= 333:y 330:. 327:{ 324:= 321:a 298:2 295:, 292:1 289:{ 286:= 283:a 261:; 258:y 252:; 249:x 243:{ 204:; 195:; 189:} 186:; 177:; 168:{ 121:; 112:; 103:{

Index

C programming language
composite
record
data type
identifier
pointer
assemblers
sizeof
primitive data type
word
ALGOL 68
typedef
statically allocated
dereferences
C++
class
visibility
.NET
C#
Visual Basic .NET
value type
reference type
Bit field
Flexible array member
Passive data structure
Union type
"Struct memory layout in C"
Ritchie, Dennis M.
"The Development of the C Language"
doi

Text is available under the Creative Commons Attribution-ShareAlike License. Additional terms may apply.