In
computer science
Computer science is the study of computation, automation, and information. Computer science spans theoretical disciplines (such as algorithms, theory of computation, information theory, and automation) to Applied science, practical discipli ...
, a composite data type or compound data type is any
data type
In computer science and computer programming, a data type (or simply type) is a set of possible values and a set of allowed operations on it. A data type tells the compiler or interpreter how the programmer intends to use the data. Most progra ...
which can be constructed in a program using the
programming language
A programming language is a system of notation for writing computer programs. Most programming languages are text-based formal languages, but they may also be graphical. They are a kind of computer language.
The description of a programming ...
's
primitive data types
In computer science, primitive data types are a set of basic data types from which all other data types are constructed. Specifically it often refers to the limited set of data representations in use by a particular processor, which all compiled pr ...
and other composite types. It is sometimes called a structure or aggregate data type,
although the latter term may also refer to
arrays
An array is a systematic arrangement of similar objects, usually in rows and columns.
Things called an array include:
{{TOC right
Music
* In twelve-tone and serial composition, the presentation of simultaneous twelve-tone sets such that the ...
,
lists
A ''list'' is any set of items in a row. List or lists may also refer to:
People
* List (surname)
Organizations
* List College, an undergraduate division of the Jewish Theological Seminary of America
* SC Germania List, German rugby unio ...
, etc. The act of constructing a composite type is known as
composition
Composition or Compositions may refer to:
Arts and literature
*Composition (dance), practice and teaching of choreography
*Composition (language), in literature and rhetoric, producing a work in spoken tradition and written discourse, to include v ...
. Composite data types are often contrasted with scalar
variable
Variable may refer to:
* Variable (computer science), a symbolic name associated with a value and whose associated value may be changed
* Variable (mathematics), a symbol that represents a quantity in a mathematical expression, as used in many ...
s.
C/C++ structures and classes
A
struct
is
C's and
C++
C++ (pronounced "C plus plus") is a high-level general-purpose programming language created by Danish computer scientist Bjarne Stroustrup as an extension of the C programming language, or "C with Classes". The language has expanded significan ...
's notion of a composite type, a datatype that composes a fixed set of labeled fields or members. It is so called because of the
struct
keyword used in declaring them, which is short for ''structure'' or, more precisely, ''user-defined data structure''.
In C++, the only difference between a
struct
and a
class
Class 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 differentl ...
is the default
access level
In computer science and computer programming, access level denotes the set of permissions or restrictions provided to a data type. Reducing access level is an effective method for limiting failure modes, reducing debugging time, and simplifying ...
, which is ''private'' for classes and ''public'' for
struct
s.
Note that while classes and the
class
keyword
Keyword may refer to:
Computing
* Keyword (Internet search), a word or phrase typically used by bloggers or online content creator to rank a web page on a particular topic
* Index term, a term used as a keyword to documents in an information syste ...
were completely new in C++, the
C programming language
''The C Programming Language'' (sometimes termed ''K&R'', after its authors' initials) is a computer programming book written by Brian Kernighan and Dennis Ritchie, the latter of whom originally designed and implemented the language, as well as ...
already had a raw type of
struct
s. For all intents and purposes, C++
struct
s form a
superset
In mathematics, set ''A'' is a subset of a set ''B'' if all elements of ''A'' are also elements of ''B''; ''B'' is then a superset of ''A''. It is possible for ''A'' and ''B'' to be equal; if they are unequal, then ''A'' is a proper subset of ...
of C
struct
s: virtually all valid C
struct
s are valid C++
struct
s with the same semantics.
Declaration
A
struct
declaration consists of a list of fields, each of which can have any type. The total storage required for a
struct
object is the sum of the storage requirements of all the fields, plus any internal
padding
Padding is thin cushioned material sometimes added to clothes. Padding may also be referred to as batting when used as a layer in lining quilts or as a packaging or stuffing material. When padding is used in clothes, it is often done in an attempt ...
.
For example:
struct Account ;
defines a type, ''referred'' to as
struct Account
. To create a new variable of this type, we can write
struct Account myAccount;
which has an integer component, accessed by
myAccount.account_number
, and a floating-point component, accessed by
myAccount.balance
, as well as the
first_name
and
last_name
components. The structure
myAccount
contains all four values, and all four fields may be changed independently.
Since writing
struct Account
repeatedly in code becomes cumbersome, it is not unusual to see a
typedef
statement in C code to provide a more convenient synonym for the
struct
. However, some programming style guides advise against this, claiming that it can obfuscate the type.
For example:
typedef struct Account_ Account;
In C++ code, the
typedef
is not needed because types defined using
struct
are already part of the regular namespace, so the type can be referred to as either
struct Account
or simply
Account
.
As another example, a three-dimensional Vector composite type that uses the floating point data type could be created with:
struct Vector ;
A variable named
velocity
with a
Vector
composite type would be declared as
Vector velocity;
Members of the
velocity
would be accessed using a dot notation. For example,
velocity.x = 5;
would set the
x
component of
velocity
equal to 5.
Likewise, a color structure could be created using:
struct Color ;
In 3D graphics, you usually must keep track of both the position and color of each vertex. One way to do this would be to create a
Vertex
composite type, using the previously created
Vector
and
Color
composite types:
struct Vertex ;
Instantiation
Create a variable of type
struct Vertex
using the same format as before:
Vertex v;
Member access
Assign values to the components of
v
like so:
v.position.x = 0.0;
v.position.y = 1.5;
v.position.z = 0.0;
v.color.red = 128;
v.color.green = 0;
v.color.blue = 255;
Primitive subtype
The primary use of
struct
is for the construction of complex datatypes, but sometimes it is used to create primitive
structural
A structure is an arrangement and organization of interrelated elements in a material object or system, or the object or system so organized. Material structures include man-made objects such as buildings and machines and natural objects such a ...
subtyping
In programming language theory, subtyping (also subtype polymorphism or inclusion polymorphism) is a form of type polymorphism in which a subtype is a datatype that is related to another datatype (the supertype) by some notion of substitutabilit ...
. For example, since
Standard C
ANSI C, ISO C, and Standard C are successive standards for the C programming language published by the American National Standards Institute (ANSI) and ISO/IEC JTC 1/SC 22/WG 14 of the International Organization for Standardization (ISO) and the ...
requires that if two structs have the same initial fields, those fields will be represented in the same way, the code
struct ifoo_old_stub ;
struct ifoo_version_42 ;
void operate_on_ifoo(struct ifoo_old_stub *);
struct ifoo_version_42 s;
. . .
operate_on_ifoo(&s);
will work correctly.
Type signature
Type signature
In computer science, a type signature or type annotation defines the inputs and outputs for a function, subroutine or method. A type signature includes the number, types, and order of the arguments contained by a function. A type signature is typ ...
s (or
Function type In computer science and mathematical logic, a function type (or arrow type or exponential) is the type of a variable or parameter to which a function has or can be assigned, or an argument or result type of a higher-order function taking or returni ...
s) are constructed from primitive and composite types, and can serve as types themselves when constructing composite types:
typedef struct Point;
typedef double (*Metric) (Point p1, Point p2);
typedef struct Circle;
See also
*
Object composition
In computer science, object composition and object aggregation are closely related ways to combine objects or data types into more complex ones. In conversation the distinction between composition and aggregation is often ignored. Common kind ...
*
struct (C programming language)
A struct in the C programming language (and many derivatives) is a composite data type (or record) declaration that defines a physically grouped list of variables under one name in a block of memory, allowing the different variables to be accessed ...
*
Scalar (mathematics)
A scalar is an element of a field which is used to define a ''vector space''.
In linear algebra, real numbers or generally elements of a field are called scalars and relate to vectors in an associated vector space through the operation of scala ...
References
{{Data types
Data types
Type theory
Articles with example C code
Articles with example C++ code