2. Embedded SQL for C : C Variables and Data Types : Variable and Type Declarations : Structure Declarations Syntax
 
Share this page                  
Structure Declarations Syntax
A C structure declaration has three variants, depending on whether it has a tag and/or a body. The following sections describe these variants.
A Structure with a Tag and a Body
The syntax of a C structure declaration with a tag and a body is:
struct tag_name {
      structure_declaration {structure_declaration}
}
where structure_declaration is:
type_specification member {, member};
In the context of a simple variable declaration, the syntax is:
struct tag_name {
      structure_declaration {structure_declaration}
} [structure_variable_name];
In the context of a type declaration, the syntax is:
typedef struct tag_name {
     structure_declaration {structure_declaration}
} structure_type_name;
Note:   
Wherever the keyword struct appears, the keyword union can appear instead. The preprocessor treats them as equivalent.
Each member in a structure_declaration has the same rules as a variable of its type. For example, as with variable declarations, the type_specification of each member must be a previously defined type or another structure. Also, you can precede the member name by asterisks or follow it with brackets. Because of the similarity between structure members and variables, the following discussion focuses only on those areas in which they differ.
struct person
{
   charname[40];
   struct
   {
      int day, month, year;
   } birth_date;
 } owner;
The preprocessor permits an initial value after each member name. Do not, however, put one there, because it causes a compiler syntax error.
If you do not specify structure_variable_name, the declaration is considered a declaration of a structure tag.
You can initialize a structure variable, but the preprocessor does not verify that the initial value is a structure aggregate.
Example: Structure with tags and body
# define MAX_EMPLOYEES 1500

typedef struct employee
{
       char name[21];
       short age;
       double salary;
 } employee_desc;
 employee_desc employees[MAX_EMPLOYEES];
 employee_desc *empdex = &employees[0];
A Structure with a Body and No Tag
The syntax of a C structure declaration with a body and no tag is:
struct {
      structure_declaration {structure_declaration}
}
where structure_declaration is the same as in the previous section.
In the context of a simple variable declaration, the structure's syntax is:
struct {
      structure_declaration {structure_declaration}
} structure_variable_name;
In the context of a type declaration, the structure's syntax is:
typedef struct {
     structure_declaration {structure_declaration}
} structure_type_name;
Note:   
All common clauses have the same rules as in the previous section. For example, struct and union are treated as equivalent, and the same rules apply to each structure member as to variables of the same type.
Specify the structure_variable_name when there is no tag. The actual structure definition applies only to the variable being declared.
Example: Structure with body and no tag
# define MAX_EMPLOYEES 1500
Struct
{
        char name[21];
        short age;
        double salary;
 } employees[MAX_EMPLOYEES];
A Structure with a Tag and No Body
The syntax of a C structure declaration with a tag and no body is:
struct tag_name
In the context of a simple variable declaration, the syntax is:
struct tag_name structure_variable_name;
In the context of a type declaration, the syntax is:
typedef struct tag_name structure_type_name;
Note:   
All common clauses have the same rules as in the previous section. For example, struct and union are treated as equivalent, and you can initialize the structure without the preprocessor checking for a structure aggregate.
The tag_name must refer to a previously defined structure or union. The preprocessor does not support forward structure declarations. Therefore, when referencing a structure tag in this type of declaration, you must have already defined the tag. In the declaration below, the tag new_struct must have been previously declared:
typedef struct new_struct *NEW_TYPE;
Example: Structure with tag and no body
union a_name
{
   char nm_full[30];
   struct
   {
          char nm_first[10];
          char nm_mid[2];
          char nm_last[18];
    } nm_parts;
 };
union a_name empnames[MAX_EMPLOYEES];