structure

Here in this we will discuss following,

 

structure

Just as an array is a collection of similar data types, similarly structure is collection of similar and different data types.

let’s explore it,

structure vs array

where in an array variable we can store only one type of values at a time, while in the structure variable we can store different type values. as you can see in below diagram,

using array

array

using structure,

structure

means, only numeric in int, character in char, Decimal in float, same Data Type values are stored in array. Whereas different type (int, char, float) values are stored together in structure.

 


Rule of declaring a structure

  • Structure are user defined data types. Structure name is an identifier i.e any one suitable name (except reserved keyword).
struct student{
  ............;
  ............;
 };

here is student is identifier,and struct is a reserve keyword.

  • The data type in the structure variable is declared within the parentheses bracket. Those are called data members and each member terminates with semicolon.
struct student {
  int roll;
  char name[20]; 
 };
  • In C++, members of a structure can have both type of data and function.
struct student {
      int roll;
      void putdata();
   };
  • Structure will reserve space in structure memory only after variable declaration. Data members of a structure are accessed using their name using the structure variable.
   structure structure_variable;
   structure_variable.roll;
   structure_variable.putdata()

Structure takes different space location for each member, in memory (memory-size of the member depends on their data type), To say means, the memory address of all the members is different.

As you can see in the diagram below,

structure-memory

In this way, store values do not affect each other due to the different memory address of the members of the structure.

Structure members Declaration

We use struct keyword to make a variable variable type of structure –

SYNTEX:

storage-class struct structure_name {
   
    data member1;
   data member2;
   data member n;

};

where structure_name is an identifier that is optional and data members are data type (int, char, float). like-

struct {
  int sr;
  char name[10];
  float price;
};

Declaration of a structure-variable

To access members of a structure, we declare structure-variable. A structure can be more than one structure-variable but by different name.

struct {
    int sr;
    char name[10];
    float price;
 } vr1,vr2,v3;

or

structure-name vr1,vr2,vr3;

Where vr1, vr2, vr3 are three structure-variables that are identifiers.

 

accessing the data member of a structure

Each data member of the structure is accessed by a structure-variable.
We can access Structure member with two types of operator-

members of the same structure are accessed using these operators with structure variables. Remember variables of a structure can be more than one.

When we access the members of a structure from arrow operator, we have to make the structure-variable a pointer variable, that is, you use the pointer to access the data members of the structure. Therefore, for a structure we usually use dot operator.

SYNTEX:-

structure_variable.member_name;

where . is a dot operator.

Let’s try with an example here

store book detail and display them using structure.

program,

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdio.h>

void main()
{
  struct { // struct declaration in local
    int sr;
    char name[20];
    float price;
} vr;

  clrscr();
  cout<<"Enter Serial No.: ";
  cin>>vr.sr;
  cout<<"Enter Book Name : ";
  gets(vr.name);
  cout<<"Enter Book Price: ";
  cin>>vr.price;
  
   cout<<"\nSerial No.: "<<vr.sr;
  cout<<"\nBook Name : "<<vr.name;
  cout<<"\nBook Price: "<<vr.price;
 
  getch();
}
OUTPUT:-
Enter Serial No.: 101
Enter Book Name : Programing
Enter Book Price: 350

Serial No.: 101
Book Name : Programing
Book Price: 350

 

initialization of a structure

As we know that the structure name is optional but when we initialize the structure, then the structure name becomes mandatory.

The initialization of the structure is slightly different from the ordinary variables. In this, after the first structure members declare, variables of structure outside the parentheses bracket are declared. Then using the structure variable, assigns values to the parentheses bracket in the same sequence in which the data is declared.

Its syntex is given below-

struct_name structure_variable = { list }vr;

Here vr is a structure-variable, more than one structure variable of one structure can be declared.

Example:-

First, structure is declare below, then using the structure-name and the structure-variable, within the parentheses bracket values are assigned to the same order (first int, then char, then float type),

thus int sr = 11, char name [ 20] = “tutorial” and float price = 230.50.

struct book= {int sr, char name[20], float price}vr;

same order

book vr = { 11, "tutorial" , 230.50};

Example:-

struct book = {int sr, char name[20], float price}vr;
book vr = { 11,”tutorial”,230.50}vr;

as we know access members of a structure like this –

vr.sr;// 11
vr.name; //tutorial
vr.price;//230.50

let’s try with an example, where we initialize structure’s member and access them.

#include<iostream.h>
#include<conio.h>

struct book {        // structure declare in global
  int sr; 
  char name[20];
  float price;
};

 book vr={11,"C++ tutorial",230.50};

void main()
{
  clrscr(); 
  cout<<"Serial No.: ";
  cout<<vr.sr;
  cout<<"\nBook Name : ";
  cout<<vr.name;
  cout<<"\nBook Price: ";
  cout<<vr.price;

  getch();
}
OUTPUT:-
Serial No.: 11
Book Name : C++ tutorial
Book Price: 230.5

 

Related Exercise

more about structure 


previous-array and their types

next-union in C++