Structures in C++

Structure is a data-type in which the individual elements can differ in data-type. Thus, a single structure might contain integer elements, floating-point elements and character elements. Pointers, arrays and other structures can also be included as elements within a structure. The individual structure elements are referred to as members.

Defining of Structures in C++:
In general terms, the composition of a structure can be defined as:

storage-class struct tag{
 data-type 1 member 1;
 data-type 2 member 2;
 ...
 data-type 2 member n;
};

In the above declaration,
storage-class is an optional storage-class specifier,
struct ia a required keyword,
tag is a name (i.e., identifier) that identifies structure of this type, and
data-type 1, data-type 2, …, data-type n represents the data-types of the members member 1, member 2, …, member n respectively.

Once the composition of the structure has been defined, individual structure-type variables can be declared as follows:

storage-class struct tag variable 1, variable 2, ..., variable n;

where storage-class is an optional storage class specifier,
struct is a required keyword,
tag is the name that appeared in the structure declaration, and
variable 1, variable 2, …, variable n are structure variables of the type tag.

Processing of Structures in C++:
The members of a structure are usually processed individually, as separate entities. Therefore, we must be able to access the individual structure members. A structure member can be accessed by writing:

variable.member

where variable refers to the name of a structure-type variable, and
member refers to the name of the member within the structure.

Notice the period (.) that separates the variable name from the member name. This period is an operator.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *