by Dinesh Thakur Category: Array Pointer and Union

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 recordl,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

 

}recordl,record2;

 

Here, the structure variables recordl, 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.

                 Memory representation of Record

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

{

I I same as in Example above

}record1={'A',10,5,23.52}

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_variab1e.member_name

To understand the concept of accessing structure members, consider these statements

Cout<<record1.grade; // To display grade value of structure

II 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

 

#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;

The output of the program is

 

Values of studl 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.

 

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=recordl ; // structure assignment

In this example, a structure variable recordl is assigned to another structure variable record2 of same structure type sales_record. The values of structure members of recordl are assigned to the corresponding members of record2.

 



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Related Articles