C++ Structures

Structure in C++ programming language groups logically related information of different data types under a single name.

A structure variable can store multiple variables of different data types. Each variable declared in structure is called member. All member variables of a structure are stored in contiguous memory locations.

Suppose, we want to store information about all employees of a company. We want to store following information for every employee:

Name (String)
Age (integer)
Department (String)
Salary (floating point number)

We can create a custom data type, which can store all related information of an employee under a single name. C++ programming language provide support for defining a custom data type(structure) which can encapsulate and store all related information about an employee.

By using a structure, we can store all the details an employee into a structure variable, and then we can define an array of structures to store details of all employees.


How to declare a structure in C++ programming

The struct Keyword is used to declare a structure type. Declaration of a structure specifies the format of the user defined data type. Like any other variables in C, a structure must be declared, before it's first use in program.

Syntax of Declaring a Structure
struct structure_name
{
    data_type variable_name1;
    data_type variable_name2;
    .
    .
    data_type variable_nameN;
};
  • struct keyword is used to declare a structure.
  • structure_name is the name of the structure. It is a valid C++ identifier.
  • data_type variable_name1; is member declaration statement. data_type is the type of member variable.
  • We can declare any number of member variables inside a structure.
  • A structure definition is only schema for creating variables. It specifies that what data a structure variable can store.
  • Declaring a structure alone will not create any variable or allocate any memory. Memory is allocated first time when a variable of structure type is declared.
  • Structure declaration ends with a semicolon(;).
For Example : Structure declaration to store employee details
struct employee
{
 char name[100];
 int age;
 char department[50];
 float salary;
};

How to define a structure variable

Once we declare the format of structure, We can define a variable of structure . There are two ways of declaring a structure variable:

Defining variables at the time of structure declaration.
For Example :
struct employee
{
    char name[100];
 int age;
 float salary;
 char department[50];
} employee_one;
variable employee_one is an instance of structure employee.
Defining variables after structure declaration.

The syntax of defining a structure variable is similar to the variable definition syntax of any basic data type in C++.

struct structure_name variable_name;
For Example :
struct employee employee_one;

How to initialize a structure variable

In C++, to initialize a structure variable We can specify initial values as a comma separated list of constants enclosed between curly braces. First value in the list gets assigned to first member of structure and second value of list gets assigned to second members of structure and so on.

struct employee
{
    char name[100];
    int age;
    float salary;
    char department[50];
} employee_one = {"Jack", 30, 1234.5, "Sales"};
We can also initialize structure variable after structure declaration.
struct employee employee_one = {"Jack", 30, 1234.5, "Sales"};

It is not compulsory to initialize all the member variables of a structure. Following statement initializes only name member of structure employee and remaining members gets initialized with zero(for integer and float) and NULL(for pointers and char).

struct employee
{
    char name[100];
    int age;
    float salary;
    char department[50];
} employee_one = {"Jack"};