C++ Structures

We have gone through arrays in the previous blog. Let us recall the important points about an array. An array holds multiple values of the same data type in one variable. So the values must be of the same data type. What if you have values of different data types? What if you want to store the details of a student like name which is a string,age which is an int,marks which can be a float value and details like whether the fees has been paid or not which is again a boolean value(True or False/ Yes or No) ?

For this purpose we use structures. A structure is an user defined data type. The structure definition for the above example with name,marks,age details is defined as follows.’studentdetail’ is the name of the structure.

struct studentdetail

     string name; 
     float marks; 
     int age; 
};

Let us create a structure variable called Anand of the structure type studentdetail. Let us also give some values to the members of the structure. 

studentdetail Anand = { “Anand”, 87.9, 14 };

An alternate way to give values to structure members is using the dot notation: 

studentdetail Anand; 
Anand.name = “Anand”; 
Anand.marks = 87.9; 
Anand.age = 14; 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Powered by WordPress.com.

Up ↑

%d bloggers like this: