How to Define / Create C++ Structure

- June 23, 2018
The structure is one of the most important building block in understanding objects and classes using C++. Typically, structures are used for file processing. A file is a collection of related records. A record is a bundle of related elements (or fields or data items) of same or different data types. In C++, structure is used to define the template (skelton) of a record.

Defining A Structure

In C++, the keyword struct is used to define a structure. The general syntax to define the structure is looks like below:
     struct tag_name
          type1   var1;
          type2   var2;
struct It is a C++ keyword that is used to define Structure.
tag_name It represents structure's name that is also called structure tag. It is a type specifier used to create structure variables.
type1 type2 They represent the data types (int, float, double, char or user-defined) of data elements of the structure. Different fields may have different data types.
var1 var2 They represent variables of data types. These variable represent the members of the structure. Each element of a specific structure has unique name inside that structure.

Example of Structure

To create a structure "book_info" and having data elements: author, publisher, year and price, the statement will be as:
   struct book_info
         char author[30];
         char publisher;
         int year;
         float price;
The declaration of the structure ended by a semicolon(;). The use of semicolon is optional.

A structure can be define inside any function (such as main() or user-defined function) or outside of the functions. If it is defined inside a function then its variable can be declared only inside that function. If it is defined outside of the function then its variable can be defined anywhere in the program. Normally, structures are defined before the main() function.

How to Declare Structure Variables?

In C++, the structure variables are declared in the same way as declaration of the simple variable.
Also Read: Difference Between Simple Variable & Structure Variable.
In declaration of structure variable, the tag name of defined function is used to specify the data type.
The syntax to declare structure variable is given below:
 tag_name    var_name1, var_name2;
tag_name It represents the structure name that is already created.
var_name1, var_name2 They indicate the variables name of same structure type that are written separated with commas.
For example, to declare structure variables book1, book2 of type "book_info", the statement is:
    book_info   book1,  book2;
A space is allocated in memory for that variable to hold all members of structure. The memory occupied by a structure variable is equal to the sum of the memory occupied by each data member of the structure.
The logical representation of structure variable "book1" in memory is looks like this:
book1 ←author→ ←publisher→ ←year→←price→

How to Access Structure Elements?

An element (member) of the structure can be access by using the dot operator that is called member access operator.
Any element of the structure can be accessed by the following rules:
  • The structure variable name should be written in the left side of dot operator.
  • The structure element name should be written in the right side of the dot operator.
The basic syntax to access the structure elements, looks like this:
Variable It indicates the name of structure variable.
Element It indicates the name of element of structure that is to be accessed.
For example, to assign the value to the element "author" of structure variable "book1" the assignment statement will be: = Arnold;
In the same way, to assign value to member "year" of structure variable "book1" the assignment statement will be:
    book1.year = 2018;
The following statements input value from the user into data members of structure variable "book1":
The following piece of code displays the value of data members of structure variable "book1":
   cout<<"Author: "<< <<endl;
   cout<<"Year: "<<book1.year <<endl;

C++ Program Using Structure

The following program defines a structure "clock" that has following members of int data type:
h, m, and s
It declares the structure variable "pst", input data into data members and displays time on the screen in standard time format.
   using namespace std;
      struct clock
          int h, m, s;
      time st;
      cout<<"Enter hours: ";
      cout<<"Enter minutes: ";
      cout<<"Enter seconds:";
      cout<<"Time is: "<<st.h<<":" <<st.m<<":"<<st.s;

Leave your reply.