Structure Definitions

Structures are aggregate data typesthat is, they can be built using elements of several types including other structs. Consider the following structure definition:


struct Card
{
 char *face;
 char *suit;
}; // end struct Card

Keyword struct introduces the definition for structure Card. The identifier Card is the structure name and is used in C++ to declare variables of the structure type (in C, the type name of the preceding structure is struct Card). In this example, the structure type is Card. Data (and possibly functionsjust as with classes) declared within the braces of the structure definition are the structure's members. Members of the same structure must have unique names, but two different structures may contain members of the same name without conflict. Each structure definition must end with a semicolon.

Common Programming Error 22.1

Forgetting the semicolon that terminates a structure definition is a syntax error.

The definition of Card contains two members of type char *face and suit. Structure members can be variables of the fundamental data types (e.g., int, double, etc.) or aggregates, such as arrays, other structures and or classes. Data members in a single structure definition can be of many data types. For example, an Employee structure might contain character-string members for the first and last names, an int member for the employee's age, a char member containing 'M' or 'F' for the employee's gender, a double member for the employee's hourly salary and so on.

A structure cannot contain an instance of itself. For example, a structure variable Card cannot be declared in the definition for structure Card. A pointer to a Card structure, however, can be included. A structure containing a member that is a pointer to the same structure type is referred to as a self-referential structure. We used a similar constructself-referential classesin Chapter 21, Data Structures, to build various kinds of linked data structures.

The Card structure definition does not reserve any space in memory; rather, it creates a new data type that is used to declare structure variables. Structure variables are declared like variables of other types. The following declarations

Card oneCard;
Card deck[ 52 ];
Card *cardPtr;

declare oneCard to be a structure variable of type Card, deck to be an array with 52 elements of type Card and cardPtr to be a pointer to a Card structure. Variables of a given structure type can also be declared by placing a comma-separated list of the variable names between the closing brace of the structure definition and the semicolon that ends the structure definition. For example, the preceding declarations could have been incorporated into the Card structure definition as follows:

struct Card
{
 char *face;
 char *suit;
} oneCard, deck[ 52 ], *cardPtr;

The structure name is optional. If a structure definition does not contain a structure name, variables of the structure type may be declared only between the closing right brace of the structure definition and the semicolon that terminates the structure definition.

Software Engineering Observation 22.1

Provide a structure name when creating a structure type. The structure name is required for declaring new variables of the structure type later in the program, declaring parameters of the structure type and, if the structure is being used like a C++ class, specifying the name of the constructor and destructor.

The only valid built-in operations that may be performed on structure objects are assigning a structure object to a structure object of the same type, taking the address (&) of a structure object, accessing the members of a structure object (in the same manner as members of a class are accessed) and using the sizeof operator to determine the size of a structure. As with classes, most operators can be overloaded to work with objects of a structure type.

Structure members are not necessarily stored in consecutive bytes of memory. Sometimes there are "holes" in a structure, because some computers store specific data types only on certain memory boundaries, such as half-word, word or double-word boundaries. A word is a standard memory unit used to store data in a computerusually two bytes or four bytes and typically four bytes on today's popular 32-bit systems. Consider the following structure definition in which structure objects sample1 and sample2 of type Example are declared:

struct Example
{
 char c;
 int i;
} sample1, sample2;

A computer with two-byte words might require that each of the members of Example be aligned on a word boundary (i.e., at the beginning of a wordthis is machine dependent). Figure 22.1 shows a sample storage alignment for an object of type Example that has been assigned the character 'a' and the integer 97 (the bit representations of the values are shown). If the members are stored beginning at word boundaries, there is a one-byte hole (byte 1 in the figure) in the storage for objects of type Example. The value in the 1-byte hole is undefined. If the member values of sample1 and sample2 are in fact equal, the structure objects are not necessarily equal, because the undefined 1-byte holes are not likely to contain identical values.

Figure 22.1. Possible storage alignment for a variable of type Example, showing an undefined area in memory.

Common Programming Error 22.2

Comparing structures is a compilation error.


Portability Tip 22.1

Because the size of data items of a particular type is machine dependent, and because storage alignment considerations are machine dependent, so too is the representation of a structure.


Introduction to Computers, the Internet and World Wide Web

Introduction to C++ Programming

Introduction to Classes and Objects

Control Statements: Part 1

Control Statements: Part 2

Functions and an Introduction to Recursion

Arrays and Vectors

Pointers and Pointer-Based Strings

Classes: A Deeper Look, Part 1

Classes: A Deeper Look, Part 2

Operator Overloading; String and Array Objects

Object-Oriented Programming: Inheritance

Object-Oriented Programming: Polymorphism

Templates

Stream Input/Output

Exception Handling

File Processing

Class string and String Stream Processing

Web Programming

Searching and Sorting

Data Structures

Bits, Characters, C-Strings and structs

Standard Template Library (STL)

Other Topics

Appendix A. Operator Precedence and Associativity Chart

Appendix B. ASCII Character Set

Appendix C. Fundamental Types

Appendix D. Number Systems

Appendix E. C Legacy Code Topics

Appendix F. Preprocessor

Appendix G. ATM Case Study Code

Appendix H. UML 2: Additional Diagram Types

Appendix I. C++ Internet and Web Resources

Appendix J. Introduction to XHTML

Appendix K. XHTML Special Characters

Appendix L. Using the Visual Studio .NET Debugger

Appendix M. Using the GNU C++ Debugger

Bibliography



C++ How to Program
C++ How to Program (5th Edition)
ISBN: 0131857576
EAN: 2147483647
Year: 2004
Pages: 627

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net