A structure can be used in a program only if memory is allocated to it. The structure definition informs the compiler about the type and the number of data members in the structures. However, it does not allocate any memory for the structure. To use a structure in a program efficiently, a structure variable needs to be declared. The syntax for declaring a structure variable is
structure_name structure_variable;
For example, the two structure variables record1 and record2 for the structure sales_record can be declared as
sales_record record1,record2;
Instead of using a separate declaration statement, the structure variables can also be declared at the time of structure definition. To understand this concept, consider
Example , which is modified as shown here.
struct sales_record { //same as }record1,record2;
Here, the structure variables record1, record2 are declared along with structure definition.
Note that providing the structure name or tag while defining it is optional. However, in this case, the structure variables can be declared only with the structure definition and not in the program. That is, while defining the structure, either the structure name or the variable declaration can be omitted but not both.
When a structure variable is declared, the C++ compiler automatically allocates sufficient memory for accommodating all the members of the structure variable. For example, when the structure variable record1 of structure sales_record is declared, the compiler allocates 9 bytes (1 byte for grade, 2 bytes for item_code, 2 bytes for item_quantity and 4 bytes for item_rate) of memory space to record1.Note that each structure variable contains its own copy of structure members and all the structure members occupy adjacent memory locations.
After declaring structure variables, the next step is to initialize structure variables. Like arrays, a structure variable can also be initialized at the time of its declaration. The syntax for initializing a structure variable is
structure_name structure_variable = {value1,value2,…… ,value n};
The values are assigned in the order in which they are listed. That is the first value is assigned to the first member, second value to the second member and so on. For example, the members of the structure variable record1 of the structure type sales_record can be declared and initialized with the help of this statement.
sales_record record1= {'A' ,10,5,23.52};
Note that if the structure variable record1 is declared along with structure definition, then it can be initialized as shown here.
struct sales_record { //same as in Example above }record1={'A',10,5,23.52}
We’ll be covering the following topics in this tutorial:
Accessing Structure Members
Once a structure variable is declared and initialized, the individual structure members can be accessed with the help of dot operator(‘ . ‘). The syntax for accessing the structure members is
structure_variable.member_name
To understand the concept of accessing structure members, consider these statements
cout<<record1.grade; // To display grade value of structure // variable record1
record1.item_code=101;//Assigning value to structure member // item_code of record1
In these statements, the members of record1 are accessed with the help of the dot operator.
The basics of structures can be summarized in a single program as shown in this example.
Example : A program to define a structure stud, declare and initialize its variable with some values and then display the values
The output of the program is
Values of stud1 are:
Roll no.: 23
Marks: 85
Result: pass
In this example, a structure stud is defined with members rollno, marks and result. Then the structure variable studl is declared and initialized at the same time. The members of studl are accessed with the help of the dot operator and their values are displayed.
Structure Assignment
The values of structure members of one structure variable can be assigned to the corresponding members of another structure variable using a single assignment statement. However, separate assignment statements are used in case of assigning values to only some structure members. Note that structure variables must belong to the same structure type. If an attempt is made to-assign a variable of one structure type to a variable of another structure type, a compile-time error is generated.
#include<iostream> using namespace std; struct stud //structure definition { int rollno; int marks; char result[5]; //array within structure } ; int main() { stud studl={23, 85, "pass"}; //declaring and initializing //structure variable cout<<endl<<"Values of studl are: "<<endl; cout<< "Roll no.: "<<studl.rollno<<endl; cout<<"Marks: "<<studl.marks<<endl; cout<<"Result: "<<studl.result<<endl; return 0;
To understand the concept of structure assignment, consider this example.
Example : A code segment to demonstrate structure assignment
struct sales_record { //same as } ; sales_record record1 = {'A',l0,5,23.52}; // declaration and initialization sales_record record2 ; // declaring another structure variable record2 = record1 ; // structure assignment
In this example, a structure variable record1 is assigned to another structure variable record2 of same structure type sales_record. The values of structure members of record1 are assigned to the corresponding members of record2.