Struct in C++

Do not miss this exclusive book on Binary Tree Problems. Get it now for free.

Reading time: 20 minutes | coding time: 20 minutes

struct or Structure is a user defined data type which allows you to combine data items of different kinds.
Structures are used to group together different data elements (types of variables) under the same name. These data elements, known as members, can have different types and different lengths.

Declaring a structure

To define a structure,we use the key word struct.

Syntax

struct structure_name {
		datatype member_name1;
		datatype member_name2;
        .
        .
        datatype member_nameN;
	};

Note: the ; after the last curly bracket is compulsary .

Example

struct Student
{
    char s_Name[30];
    int s_RollNo;
    int s_Age;
};

Note: This declaration creates a new type (Student).

Accessing data members of a structure

The data members of a structure can't be accessed directly.They can only be accessed using the variable of a structure using member access operator (.).
The member access operator is used between the structure variable name/ and the structure member that we wish to access.
You can use struct keyword to define variables of structure type but it is optional in C++.

Declaring a structure variable:

1.You can simply declare a structure variable using the key word struct ,structure name and variale name.
Syntax

struct structure_name variable_name;
or
structure_name variable_name;

2.You can declare structure variable at the time of struction declaration.

 struct structure_name {
   	datatype member_name1;
   	datatype member_name2;
       .
       .
       datatype member_nameN;
   } variable_name1,variable_name2,....variable_nameN;

Example

struct Student S1;
or
Student S1;
or
struct Student
{
    char s_Name[30];
    int s_RollNo;
    int s_Age;
} S1,S2;

Size of a structure

When a structure is declared there is no memory space allocated to it.It's just a template.A structure takes up space only when the variable of the structure is declared.

The total size of the structure is the sum of size of the data members in the structure

The size of a structure can be checked as as follows-

struct Student
{
    char s_Name[30];
    int s_RollNo;
    int s_Age;
} S1,S2;
int main()
{ cout<<"Size ="<<sizeof(S1);
return 0;
}

Output

Size=38

Note : The size of struct is 38 btyes(30 for name(30 char)+ 4 for rollno(int)+4 for age(int))

Accessing structure members:

Syntax

variable_name.member_name=value;
or
cin>>variable_name.member_name;
cout<<variable_name.member_name;

Example

struct Student S1;

		S1.s_Name = "Jany";
		S1.s_RollNo = 10;
        cin>>S1.s_Age;
        cout << "Name: " << S1.s_Name << '\n';
		cout << "Rollno: " << S1.s_RollNo<< '\n';
        cout << "Age: " << S1.s_Age; //(let age be 20)

Output:

Name: Jany
Rollno: 10
Age: 20

Pointer to Structure

Like any other type, structures can be pointed to by its own type of pointers.

Syntax

struct structure_name *structure_pointer;

Example

struct Student *S1;

Accessing members through structure pointers
If you have a pointer to a structure and you want to access the members of the structure then you have to use the -> (infix operator/arrow operator) instead of a dot operator.

Syntax

variable_name->member_name=value;
or
cin>>variable_name->member_name;
cout<<variable_name->member_name;

Example

struct Student *S2;

		S2->s_Name = "Richard";
		S2->s_RollNo = 11;
        cin>>S2->s_Age;
        cout << "Name: " << S2->s_Name << '\n';
		cout << "Rollno: " << S2->s_RollNo<< '\n';
        cout << "Age: " << S2->s_Age; //(let age be 21)

Output:

Name: Richard
Rollno: 11
Age: 21

Sign up for FREE 3 months of Amazon Music. YOU MUST NOT MISS.