C++ Structure

C++ Structure

Array is a collection of variables of same data types as to store roll numbers of 10 students, better to use array of size 10 instead of using 10 variables.

Suppose we have to store details of 10 student as student name, roll number and cgpa where data type of each is different. For this we use structure. So we can say that..

Structure in C/C++ is a collection of variables of same or different data types under a single name

Structure declaration in C/C++

Structure can be declared either inside main function or before main function. Syntax of struct declaration is as below

struct structure-name{

    data members

};

Below is the code for declaring a structure named Student. 

struct Student{
    string name;
    int rollNo;
    float cgpa;
};

Student structure contains three variables i.e name, rollNo and cpga. These variables are called data members.

Structure is also a data type but unlike int, float, char etc which are primitive data type, structure is user defined data type.

Create object of structure

We can create object of structure Student as below

Student s1;

Pointer to structure

We can declare pointer to structure like

Student s1,*ptrs1;

Here ptrs1 is pointer to structure 

We can access data members of structure using object or pointer to structure.

Array of structure

We can declare array of structure like

Student sArr[5];

Here sArray is array of structures of 5 students

We can fill this array with objects of students as below

sArr[0]={"laim",12,3.5};
sArr[1]={"john",43,3.7};

Or

Student s1;
s1.name = "laim";
s1.rollNo = 1234;
s1.cgpa = 3.45;
sArray[0= s1;

We can display data members of objects in array like this

cout<<sArray[0].name;
cout<<sArray[0].rollNo;
cout<<sArray[1].name;
cout<<sArray[1].rollNo;
cout<<sArray[2].name;
cout<<sArray[2].rollNo;


Structure initialization

Structure initialization means to initialize the data members of a structure. There are three methods to initialize data members of structure


First Method: At the time of object creation

By this method object is created and data members are initialized in a single line as below

Student s1={
    "john",
    234,
    3.57
};

First data member name is initialized by  "ali"

Second data member rollNo is initialized by 231

Third data member cgpa is initialized by 3.2


Second Method: Using dot (.) operator with object

Using this method,  first structure object is created as below

Student s1;

Then using this object s1, data members are initialized as below

s1.name = "john";
s1.rollNo = 1234;
s1.cgpa = 3.45;


Third Method: Using arrow operator (->) with pointer

In this method, pointer to structure is created as below

Student s1, *ptrs1;
ptrs1 = &s1;

Then with help of arrow operator with pointer, data members are initialized as below

ptrs1->name = "john";
ptrs1->rollNo = 1234;
ptrs1->cgpa = 3.45;

We can use dot operator with pointer to structure. For this, dereferencing the pointer is necessary as below

(*ptrs1).name = "john";
(*ptrs1).rollNo = 1234;
(*ptrs1).cgpa = 3.45;

Structure and function

We can pass structure as argument to a function. A structure can be passed either by value or by reference.

Structure pass by value

void print(Student s)
{
    cout<<s.name;
}
Student s1={
    "john",
    23,
    3.34
};
print(s1);

Output: john

Structure pass by reference

void print(Student *ptr)
{
    cout<<ptr->name;
}
Student s1={
    "john",
    23,
    3.34
};
print(&s1);

Output: john

Below is a complete C++ program to cover all concepts of structure

C++ Structure
C++ Structure
Output
C++ Structure output
C++ Structure output

Comments

Post a Comment