6.2 Dealing with Structures


Objectives:

At the end of this lesson you will be able to:

use structure

Declaring and Creating a Structure

Structures are declared in C++ using the following syntax:.



6.2 Dealing with Structures


where structure_name is a name for the structure type, object_name can be a set of valid identifiers for objects that have the type of this structure. Within braces { } there is a list with the data members, each one is specified with a type and a valid identifier as its name. The above structure declaration is also called Structure Specifier.

The first thing we have to know is that a structure creates a new type: Once a structure is declared, a new type with the identifier specified as structure_name is created and can be used in the rest of the program as if it was any other type.

Declaring structures does not mean that memory is allocated. Structure declaration gives a skeleton or template for the structure.

It is important to clearly differentiate between what is the structure type name, and what is an object (variable) that has this structure type. We can instantiate many objects (i.e. variables, like apple, banana and orange) from a single structure type (product).

The above structures declaration can be used for the next sample:


6.2 Dealing with Structures



The keyword struct introduce the definition for structure Product. The identifier Product is the structure name and is used in C++ to declare variables of the structure type. In this example, the structure type is Product. Data (and possibly functions) 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 definitions must end with a semicolon.

The definition of Product contains two members of type int* - weight and float* - price. 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 definitions can be of many data types. For example, a Student structure might contain character-string members for the first and last names, an int member for the student’s age, a char member containing ’M’ or ’F’ for the student’s gender and so on.


6.2 Dealing with Structures


A structure cannot contain an instance of itself. For example, a structure variable Product cannot be declared in the definition for structure Product. A pointer to a Product 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.

Declaring Structure Variables

The Product structure declaration above 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

declare apple, mango and orange to be a structure variable of type Product, 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.

6.2 Dealing with Structures


For example, the preceding declarations could have been incorporated into the Product structure definitions as follows:

Once declared, product has become a new valid type name like the fundamental ones int, char or short and from that point on we are able to declare objects (variables) of this compound new type, like we have done with apple, mango and orange.

When structure is defined, it allocates or reserves space in memory. The memory space allocated will be cumulative of all defined structure members. In the above example, there are 3 structure members: custnum, salary and commission. Of these, two are of type in and one is of type float. If integer space allocated by a system is 2 bytes and float four bytes the above would allo9acter 2bytes for custnum, 2 bytes for salary and 4 bytes for commission.

6.2 Dealing with Structures


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

The only valid built-in operations that may be performed on structure variable are assigning a structure variable to a structure variable of the same type, taking the address (&) of a structure object, accessing the members of a structure object (int the same manner as members of a class are accessed) and using the sizeof operatpr 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 Initialization

As with arrays and variables, structure members can also be initialized. The initializer is preceded by an equal sign (=). There are two ways to do the initialization of structures.


6.2 Dealing with Structures


      1. Structures can be initialized field by field as below:



6.2 Dealing with Structures


      2. Structures can also be initialized by a list at declaration (similar to an array). This is performed by
          enclosing the values to be initialized inside the braces { and } after the structure variable name
          while it is defined.



6.2 Dealing with Structures


Example 6.1:


6.2 Dealing with Structures


The output of the above program is


In the above example, the structure variable can be assigned to each by using assignment operator ’=’. The programmer must consider that only structure variables of the same type can be initialized. If a programmer tries to initialize two structure variables of different types to each other it would result in compiler error.

It is wrong to initialize as: