When writing a program, several variables (identifiers) are declared and used. Some of the names used as identifiers are related and can be grouped together (memory wise) in one
location as one unit in memory. This grouping of related names, including several variables and functions is called a structure. In C, structures group only variables, while in C++
structures group variables as well as their related functions. Each variable and function is referred to as a member of the structure. In C++, the bundling of variables and functions
adds the important concept of Object Oriented Programming (OOP), also known as class, to the C language. This incorporation of variables and functions makes C++ both a
Procedural as well Object-Oriented language.
The key word struct is used to create a structure. In C, there are three general formats to create a structure variable as shown in Figures 7.1a, b, and c. Notice in Figure 7.1a,
the structure is defined with the use of a tag name followed by the definition of the structure and the actual structure variable name. In Figure 7.1b, the use of a tag name is
eliminated and the rest of the definition and the structure declaration remain the same.
1. struct tagname { 2. datatype variablename; 3. datatype variablename; 4. · 5. · 6. } structurevariablename; 1. struct { 2. datatype variablename; 3. datatype variablename; 4. · 5. · 6. } strucuturevariablename; Figure 7.1a – The
definition and declaration of a structure with the use of a tag name
following the struct
keyword. Figure 7.1b – The
definition and declaration of a structure without the use of the tag name
following the struct
keyword. 1. struct tagname { 2. datatype variablename; 3. datatype variablename; 4. · 5. · 6. }; 7. struct tagname structurevariablename;
Figure 7.1c – The definition
and declaration of a structure with the use of the tag name following the struct
keyword in both the definition and the declaration.