These examples illustrate structure declarations:
struct employee /* Defines a structure variable named temp */
{
char name[20];
int id;
long class;
} temp;
The employee structure has three members: name, id, and class. The name member is a 20-element array, and id and class are simple members with int and long type, respectively. The identifier employee is the structure identifier.
struct employee student, faculty, staff;
This example defines three structure variables: student, faculty, and staff. Each structure has the same list of three members. The members are declared to have the structure type employee, defined in the previous example.
struct /* Defines an anonymous struct and a */
{ /* structure variable named complex */
float x, y;
} complex;
The complex structure has two members with float type, x and y. The structure type has no tag and is therefore unnamed or anonymous.
struct sample /* Defines a structure named x */
{
char c;
float *pf;
struct sample *next;
} x;
The first two members of the structure are a char variable and a pointer to a float value. The third member, next, is declared as a pointer to the structure type being defined (sample).
Anonymous structures can be useful when the tag named is not needed. This is the case when one declaration defines all structure instances. For example:
struct
{
int x;
int y;
} mystruct;
Embedded structures are often anonymous.
struct somestruct
{
struct /* Anonymous structure */
{
int x, y;
} point;
int type;
} w;
Microsoft Specific
The compiler allows an unsized or zero-sized array as the last member of a structure. This can be useful if the size of a constant array differs when used in various situations. The declaration of such a structure looks like this:
struct identifier{ set-of-declarations type array-name[ ];};
Unsized arrays can appear only as the last member of a structure. Structures containing unsized array declarations can be nested within other structures as long as no further members are declared in any enclosing structures. Arrays of such structures are not allowed. The sizeof operator, when applied to a variable of this type or to the type itself, assumes 0 for the size of the array.
Structure declarations can also be specified without a declarator when they are members of another structure or union. The field names are promoted into the enclosing structure. For example, a nameless structure looks like this:
struct s
{
float y;
struct
{
int a, b, c;
};
char str[10];
} *p_s;
.
.
.
p_s->b = 100; /* A reference to a field in the s structure */
See Structure and Union Members for information about structure references.
END Microsoft Specific