Previous
|
Up
|
Next
|
Function Declarators |
Epsilon Extension Language |
Complex Declarators |
Epsilon User's Manual and Reference >
Epsilon Extension Language >
Data Types >
Structures and Unions
This section describes how to define variables of
type structure of t1, ..., tn, where t1,
..., tn are each types. First, we give an informal
description, with examples, of how structures are often declared. A
more formal description with BNF diagrams follows.
There is a special type-specifier, called a structure-or-union
specifier, that defines structure and union types. This
type-specifier has several forms.
The simplest form is seen in the following example:
struct {
int field1;
char name[30];
char *data;
}
The field names of the structure are the identifiers being declared
within the curly braces. These declarations look like variable
declarations, but instead of declaring variables, they declare
field names. The type of a particular field is the type the
identifier would have if the declaration were a variable declaration.
The example above refers to a structure with fields named field1 ,
name , and data , with types int, array of char, and
pointer to char, respectively.
Use the structure-or-union specifier like the other type-specifiers
(int, short, char, and spot) in declarations. For example:
struct {
int field1;
char name[30];
char *data;
} rec, *recptr, recary[4];
declares rec to be a structure variable, recptr to be a
pointer to a structure, and recary to be an array of (4)
structures.
The structure-or-union-specifier
may contain a tag, which gives
a short name for the entire structure. For example, the
type-specifier in the following example:
struct recstruct {
int field1;
char name[30];
char *data;
};
creates a new type, struct recstruct , that refers to the structure
being defined. Given this structure tag, we may define our structure
variables in the following manner:
struct recstruct rec, *recptr, recary[4];
Structure (or union) tags also let you create self-referential types.
Consider the following:
struct list {
int data;
struct list *next;
};
struct list list1, list2;
This creates a structure type list , which has a data field
that's an int, and a next field that is a pointer to a list
structure. A structure may not contain an instance of itself, but may
contain (as in this example) a pointer to an object of its type.
More formally, a structure-or-union-specifier has the following form:
struct-or-union-specifier:
struct-or-union-tag
struct-or-union-tag { member-list }
{ member-list }
struct-or-union-tag:
identifier
member-list:
type-specifier declarator-list ;
type-specifier declarator-list ; member-list
A description of how to use structures and unions in expressions
appears in Miscellaneous Operators.
Previous
|
Up
|
Next
|
Function Declarators |
Epsilon Extension Language |
Complex Declarators |
Epsilon Programmer's Editor 14.04 manual. Copyright (C) 1984, 2021 by Lugaru Software Ltd. All rights reserved.
|