C++ Structures2 min read

In the C/C++ programming language, the structure is user-defined data types that allow us to store the combination of different data types together. All the different data type variables are represented by a single name.

Unlike an array that holds the data of the same type, the structure holds the data of different types in a contiguous memory location. It begins with the keyword struct.

Suppose, we want to keep the record of students with their name(string), roll_no(int), address(string), Score(float) and these are of different data types. So instead of creating a different variable, again and again, we can simply create members for each student with their respective data.

Syntax of struct in C++:

The keyword used to define the structure is struct.

Consider basic data of student: name, roll_no, score.
student structure is declared in the following way:


Declaring Structure variable

It means creating an instance of a structure in order to access the members of the structure. Once the structure student is declared, its variable is created by adding the following in the main function.

struct student s1, s2;

s1 and s2 are structure variables of the type Student. These are like the objects for the student just like an object in C++. When these variables are created, memory is allocated by the compiler.


Accessing Struct Members

The structure variable can be accessed by using a dot (.) operator.

Suppose you want to access the score of an s1 student and assign it with a value of 80. This work can be performed in the following way.

s1.score = 80;


Example of C++ Structure

Output:

Student 1 Information
Name: John Mac
Roll No: 101
Score: 92.15

Student 2 Information
Name: Mickey Macguel
Roll No: 102
Score: 80


After learning structure, go through the following topics in structure:


MORE

Java Program to find the sum of the Largest Forward Diagonal

in this tutorial, we will write a java program to find the sum of the Largest Forward Diagonal in an Arraylist (matrix). Java Program to …

C Program to search an element in an array using Pointers

A separate function( search_function()) will be created where the array pointer will be declared and the searched element along with the size of an array …

C Program to find the sum of the digits of a number using recursion function

This C program calculates the sum of digits of a given number using recursion. Here’s a concise explanation: Function Definition: sumDigits(int n) This function calculates …

C program to find factorial of a numberĀ using Ternary operator with Recursion

Recursion refers to the function calling itself directly or in a cycle. Before we begin, you should have the knowledge of following in C Programming: …

C Program to Add Two Numbers Using Call by Reference

The program takes the two numbers from the user and passes the reference to the function where the sum is calculated. You may go through …

Find the output ab, cd, ef, g for the input a,b,c,d,e,f,g in Javascript and Python

In this tutorial, we will write a program to find a pairs of elements from an array such that for the input [a,b,c,d,e,f,g] we will …