HOME

TheInfoList



OR:

In the C programming language, struct is the keyword used to define a composite, a.k.a. record,
data type In computer science and computer programming, a data type (or simply type) is a collection or grouping of data values, usually specified by a set of possible values, a set of allowed operations on these values, and/or a representation of these ...
a named set of values that occupy a block of memory. It allows for the different values to be accessed via a single
identifier An identifier is a name that identifies (that is, labels the identity of) either a unique object or a unique ''class'' of objects, where the "object" or class may be an idea, person, physical countable object (or class thereof), or physical mass ...
, often a 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
assemblers Assembler may refer to: Arts and media * Nobukazu Takemura, avant-garde electronic musician, stage name Assembler * Assemblers, a fictional race in the ''Star Wars'' universe * Assemblers, an alternative name of the superhero group Champions of ...
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 A word is a basic element of language that carries semantics, meaning, can be used on its own, and is uninterruptible. Despite the fact that language speakers often have an intuitive grasp of what a word is, there is no consensus among linguist ...
boundaries) is implementation-specific and may include padding. Modern compilers typically support the #pragma pack directive, which sets the size in bytes for alignment. The C struct feature was derived from the same-named concept in
ALGOL 68 ALGOL 68 (short for ''Algorithmic Language 1968'') is an imperative programming language member of the ALGOL family that was conceived as a successor to the ALGOL 60 language, designed with the goal of a much wider scope of application and ...
.


Declaration

The syntax for a struct declaration is shown by this simple example: struct tag_name ; The tag_name is optional in some contexts.


Typedef

Via the keyword typedef, a struct type can be referenced without using the struct keyword. However, some programming style guides advise against this, claiming that it can obfuscate the type. For example: typedef struct tag_name thing_t; thing_t thing; In C++ code, typedef is not needed because types defined via struct are part of the regular namespace, so the type can be referred to as either struct thing_t or thing_t.


Initialization

There are three ways to initialize a structure. For the type: struct point_t ; ''C89-style initializers'' are used when contiguous members may be given. For example: struct point_t a = ; For non contiguous or out of order members list, ''designated initializer'' style may be used. For example: struct point_t a = ; 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_t b = a;


Copy

The state of a struct can be copied to another instance. A compiler might use memcpy() to copy the bytes of the memory block. struct point_t a = ; struct point_t b; b = a;


Pointers

Pointers can be used to refer to a struct by its address. This is useful for passing a struct to a function to avoid the overhead of copying the struct. The -> operator dereferences the pointer (left operand) and accesses the value of a struct member (right operand). struct point_t point = ; int x = point.x; point.x = 10; struct point_t *pp = &point; x = pp->x; pp->x = 8;


In other languages


C++

In C++, struct is essentially the same as for C. Further, a
class Class, Classes, or The Class may refer to: Common uses not otherwise categorized * Class (biology), a taxonomic rank * Class (knowledge representation), a collection of individuals or objects * Class (philosophy), an analytical concept used d ...
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 The .NET platform (pronounced as "''dot net"'') is a free and open-source, managed code, managed computer software framework for Microsoft Windows, Windows, Linux, and macOS operating systems. The project is mainly developed by Microsoft emplo ...
languages have a feature similar to struct in C called struct in C# and Structure 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.


See also

* * * *


References

{{reflist C (programming language)