A structure contains an ordered group of data objects, and is defined with the keyword struct. It is a special type of class where the members are public by default. A structure type need only be defined once within the source file. Members of a structure are limited to data members only.
Syntax
![]()
The following example, a structure is defined for address:
struct address {
int street_number;
char street_name[25];
char city[10];
char province[2];
char postal_code[7];
};
Now, if you want to apply the same structure to business_address, one of the following declarations can be made:
struct address business_address; // explicit struct specification
or
address business_address; // In C++, the struct keyword is not necessary address *p_business_address = &business_address;
To refer to a particular field in the address structure, such as city, one of the following methods can be applied:
business_address.city = "Toronto";
p_business_address -> city = "Toronto";
Structures are identified by their type names, not by the structure composition. For example, a structure called mailing_label can be defined as having the same member specification as the struct type address but they will be viewed as being two different types. Therefore, objects of type address cannot be compared or substituted with objects of type mailing_label.
struct mailing_label {
int street_number;
char street_name[25];
char city[10];
char province[2];
char postal_code[7];
};
mailing_label home_address;
void f() {
home_address = business_address; // type mismatch error!
...
}
A structure can exist in the same scope as an object, function, or enumerator type with the same name. For example, a function named address can be declared and defined in the same scope as the structure type address. To distinguish the structure type from the function, the keyword struct should be used with the structure name whenever a declaration is made, like so:
int address (struct address*);
void f() {
struct address* work; // must be explicitly declaredd with struct
...
address(work); // ok
}
The typedef specifier cannot be used to create an alias to replace the struct address*. An explicit declaration must be used to distinguish the structure type from the function.
Anonymous Structures in C
Structures can be declared without declarators if they are
members of another structure or union. Structures without
declarators are called anonymous structures. They are permitted
in extended mode only. Extended mode can be set by specifying the
appropriate level
langauge option.
Members of an anonymous structure can be accessed as if they were declared directly in the containing structure or union. For example,
struct s {
int a;
struct {
int b;
float c;
}
}S;
you can make the following statements:
S.a = 5; S.b = 36;
An anonymous structure can be declared using one of two ways:
typedef struct {
int a;
int b;
} Struct_T;
struct s1 {
Struct_T;
int c;
} dave;
struct s1 {
int a;
int b;
};
struct s2 {
struct s1;
int c;
} dave;
Using either method, members can be accessed as dave.a, dave.b, and dave.c.
An anonymous structure must be a member of, or nested within another anonymous structure that is a member of, a named structure or union. If a structure is declared at file scope without a declarator, its members are not available to the surrounding scope. For example, the following structure only declares the tag tom:
struct tom {
int b;
float c;
};
The variables b and c from this structure cannot be used at file scope, and the following statements generate errors:
b = 5; c = 2.5;
Anonymous Structures in C++
Anonymous structures are treated differently in the C++
language. Given these declarations:
struct don {
int a;
int b;
};
struct pete {
struct don;
};
In C, struct don; declares an anonymous structure as a member of pete. In C++, it is treated as a forward or incomplete declaration of a nested structure, not as an anonymous structure.
A C++ anonymous structure is a structure without a class name. It cannot be followed by a declarator. An anonymous structure is not a type; it defines an unnamed object and it cannot have member functions. The member names of a C++ anonymous structure must be distinct from other names within the scope in which the structure is declared. You can use member names directly in the structure scope without any additional member access syntax.
An anonymous structure cannot have protected or private members. A global anonymous structure must be declared with the keyword static.
