GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing en.wikipedia.org — struct (C programming language) - Wikipedia In the C programming language, struct (referring to a structure) is the keyword used to define a composite, a.k.a. record, data type – 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. A struct can contain other data types, so it is used for mixed-data-type records. For example, a bank customer struct might contain fields for the customer's name, address, telephone number, and 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 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 The C struct feature was derived from the same-named concept in ALGOL 68.[2] DeclarationThe following simple example depicts declaring a struct named struct Foo { Type1 member1; Type2 member2; }; The added name Members may possibly be padded for memory alignment, and thus it is often recommended to order fields from largest to smallest size for efficient memory usage. A function may directly return a struct, although this is often not efficient at run-time. Since C99, a struct may also end with a flexible array member. Structs can be composed of other structs: struct Date { int year; int month; int day; }; struct Birthday { char name[50]; struct Date dob; }; It is legal to declare an anonymous struct within a struct, but it cannot be named in C (as there is no notion of nested types, unlike C++ where nesting named structs is legal). struct Person { char name[50]; int age; struct { char city[50]; int zip; } addr; }; A struct containing a pointer to a struct of its own type is commonly used to build linked data structures: struct LinkedList { void* item; // stores the current item struct LinkedList* next; // stores the next list, or NULL if nothing next }; Circularly referencing between two structs must be done using a forward declaration and pointers: struct B; struct A { struct B* b; }; struct B { struct A* a; }; TypedefMain article: Typedef
Via the keyword For example: typedef struct Foo { Type1 member1; Type2 member2; } Foo; // struct Foo can now be referred to as 'Foo' Foo foo; In C++ code, the type may be referred to as either InitializationConsidering the struct declaration: struct Point { int x; int y; }; There are three ways to initialize a structure: C89-style initializers are used when contiguous members may be given.[4] For example: struct Point a = { 1, 2 }; For non contiguous or out of order members list, the designated initializer style (introduced in C99) may be used.[5] For example: struct Point a = { .x = 1, .b = 2 }; // May initialize fields out of order too struct Point b = { .y = 4, .x = -3 }; 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: struct Point b = a; CopyThe state of a struct can be copied to another instance. A compiler might use struct Point a = { 1, 3 }; struct Point b; b = a; Bit fieldsMain article: Bit field
Structs may also use bit fields to allow fields to share the same storage units, but layouts are implementation-defined. struct Properties { // three fields can be compactly packed in one byte unsigned char visible : 1; // a occupies 1 bit unsigned char color : 3; // b occupies 3 bits unsigned char size : 4; // c occupies 4 bits }; PointersPointers can be used to refer to a struct Point p = { 3, 7 }; int x = p.x; p.x = 10; struct Point* pp = &p; x = pp->x; pp->x = 8; Functions may take a struct as a parameter by value, but this is expensive as it copies the entire struct. Meanwhile, passing it by pointer is often preferable as the size of a pointer is known (typically 4 or 8 bytes). #include <stdio.h> // a larger struct which may carry a lot of data struct Student { char name[50]; unsigned int id; unsigned int semester; float gpa; }; // passing by value void print_student(struct Student s) { printf("Name: %s, ID: %d, in semester %d, with GPA: %.2f\n", s.name, s.id, s.semester, s.gpa); } // passing by pointer void print_student(struct Student* s) { printf("Name: %s, ID: %d, in semester %d, with GPA: %.2f\n", s->name, s->id, s->semester, s->gpa); } In other languagesALGOL 68[2], Crystal (programming language),[6] D,[7] Go, Julia,[8] Rust, Swift and Zig[9] have structs. C++In C++, struct is essentially the same as for C, but also with methods. 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.NET languages have a feature similar to JavaJava does not have the See also
References
|
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |