Struct Data Type
The "struct" data type is specific to the C and C++ programming languages. The original data type came from C and then was subsumed into C++. While the C++ version retains the original semantics and syntax of the original, it has been extended to make C++ structs bona-fide objects in their own right.
Structures contain a set of contiguously allocated member variables that can be of any type, including other structures. In C++ structures may have member functions, too. C++ structures are identical to C++ classes except that the access rights to a structure's functions and data members are public rather than private unless the programmer says otherwise. The reason for this is that the conceptual difference is that a structure is a collection of related data elements whereas a class is a tightly bound cohesive unit or object.
The struct keyword is short for "structure" and is followed by one or more variable declarations within a set of braces (the "{" and "}" characters respectively). A typical C structure declaration is shown in the code fragment below:
- struct roadVehicle {
- int engineSize;
- int wheelCount;
- int seatCount;
- };
This fragment declares a structure that might represent a road vehicle. Once it has been declared, it can then be used as a type blueprint from which the programmer can create roadVehicle objects. For example the code below declares two unique and separate variables:
- struct roadVehicle car;
- struct roadVehicle truck;
The syntax is a little cumbersome, so C has the "typedef" keyword that associates a simple identifier with a complex declaration. So if the structure had been declared as
- typedef struct {
- int engineSize;
- int wheelCount;
- int seatCount;
- } roadVehicle;
it would be possible to write
- roadVehicle car;
- roadVehicle truck;
In C++ the typedef keyword is not needed here although it is supported for backwards compatibility with C; in other words it would have been possible to have the struct declared like this
- struct roadVehicle {
- int engineSize;
- int wheelCount;
- int seatCount;
- };
and then to write
- roadVehicle car;
- roadVehicle truck;
The method used to access a structure's members in both C and C++ depends on whether the structure is being accessed by value (as an object), or via a pointer. In C++ it is also possible to access a structure's members by reference, in which cases the syntax is the same as for access by value.
For example:
- roadVehicle car;
- roadVehicle truck;
- roadVehicle van;
- roadVehicle* carPtr = car
- roadVehicle& vanRef = van; // C++ only
To assign a value to the number of wheels of each vehicle the code could look like this:
- vanRef.wheelCount = 3; // must have hit a drain
- carPtrengineSize = 400;
- truck.seatCount = 2;
The only type of variable a structure cannot contain is its own type, so the following declaration is illegal:
- struct roadVehicle {
- roadVehicle vehicle;
- int engineSize;
- int wheelCount;
- int seatCount;
- };
However, it can contain a "pointer" to its own type like this:
- struct roadVehicle {
- roadVehicle* next;
- int engineSize;
- int wheelCount;
- int seatCount;
- };
Why this is useful is not perhaps immediately apparent, but on closer examination it can be seen that "next" can point to another roadVehicle, which can point to another roadVehicle, . . . and so on. Thus the structure is the basic element of linked lists in C.
A special kind of structure is a "union." A union is a set of overlaid structures that all begin at the same address. A union is really only of any use as "containers" of structures that can have more than one "view" of them. For example, a programmer may wish to store a set of integers or characters in a structure of a single type:
- typedef struct {
- int a;
- int b;
- } integers;
-
- typedef struct {
- char a;
- char b;
- } characters;
-
- typedef union {
- integers i;
- characters c;
- } values;
What this union does is give two "views" of integer values. If the programmer accesses an object of type "values" via an "integer" structure, he will get integer values out; if he access it via a "character" structure, he will get char values out.
Structures provide an extremely powerful way of building very complex data types in C and it is almost impossible to create a program of any real utility without them. Their use in C++ has been somewhat attenuated by the class declaration, but they still have their place where a full class would be considered overkill.
This is the complete article, containing 676 words
(approx. 2 pages at 300 words per page).