C Language and Libraries Help (clang.hlp) (
Table of Contents;
Topic list)
Important Notice
The pages on this site contain documentation for very old MS-DOS software,
purely for historical purposes.
If you're looking for up-to-date documentation, particularly for programming,
you should not rely on the information found here, as it will be woefully
out of date.
struct
◄Example► ◄Up► ◄Contents► ◄Index► ◄Back►
─────C/C++ Language─────────────────────────────────────────────────────────
Keyword: struct
Syntax: struct [tag] { member-list } [declarators];
[struct] tag declarators;
Summary: Defines a structure type and/or a variable of a
structure type.
See: ◄Anonymous Structures►
◄Unsized Array in a Structure►
See also: class, union, enum
A structure type is a user-defined composite type. It is composed
of "fields" or "members" that can have different types.
In C++, a structure is the same as a class except that its members
are public by default.
Declaring a Structure
Use the struct keyword with a <member-list> enclosed by braces to
declare a structure type. The <tag> identifies the type.
For both C and C++, a data item in the <member-list> can be any
valid data declaration, including another structure, or it can be
a bit field in the following form:
int-type-specifier [identifier] : constant-expression
The <int-type-specifier> must be an integral type. The
<constant-expression> specifies the number of bits in the field.
Unnamed bit fields can be used for alignment. If an unnamed field
has width 0, the next field is aligned on the current packing
boundary as defined by /Zp or the pack pragma.
See: /Zp, pack
A structure type cannot contain itself as a member. It can
contain a pointer to itself.
Using A Structure
In C, you must use the following syntax to declare a variable
as a structure:
struct tag declarator;
You have the option of declaring variables when the structure type
is defined by placing one or more comma-separated variable names
between the closing brace and the semicolon.
In C++, the struct keyword is unnecessary once the type has been
defined. This allows you to declare variables with this syntax:
tag declarator;
As with C, you have the option of declaring variables in the
declaration of the struct. For example:
struct PERSON // Declare PERSON struct type
{
int age; // Members of several types
long ss;
float weight;
char name[25];
} family_member; // Define object of type PERSON
struct PERSON sister; // C declaration of a structure
// variable
PERSON brother; // C++ declaration of a
// structure
sister.age = 13; // Assign values to members
brother.age = 7;
Structure variables can be initialized. The initialization for each
variable must be enclosed in braces. For example:
struct POINT // Declare POINT structure
{
int x; // Define members x and y
int y;
} here = { 20, 40 }; // Variable here has
// values x = 20, y = 40
struct POINT there; // Variable there has POINT type
struct CELL // Declare CELL bit field
{
unsigned character : 8; // 00000000 ????????
unsigned foreground : 3; // 00000??? 00000000
unsigned intensity : 1; // 0000?000 00000000
unsigned background : 3; // 0???0000 00000000
unsigned blink : 1; // ?0000000 00000000
} screen[25][80]; // Array of bit fields
-♦-