Structure in C++

Advertisements

Prev Tutorial Next Tutorial

Structure in C++

Structure is a user defined data type which hold/store different-different type of data Item/Element in a singe variable. In case of array we store only similar data type.

structure in c++

Declare Structure in C++

	
struct tag
{
   data_type1 member1;
   data_type2 member2;
   data_type3 member3;
};

Another syntax to Declare Structure.

Declare Structure in C++

	
struct <structure_name>
{
  data_type1 member1;
  data_type2 member2;
  data_type3 member3;
    ...
    ...
};

Why Use Structure in C++

In C++ language Array is also a user defined data type but Array hold or store only similar type of data, If we want to store different-different type of data. In this case we need to defined separate variable for each type of data. To resolve this problem we use Structure in C++ Programming, It can store different-different data type in single variable.

Example: Suppose we want to store Student record, then we need to store....

  • Student Name
  • Roll number
  • Class
  • Address

For store Student name and Address we need character data type, for store Roll number and class we need integer data type.

If you use Array, in this case you need to defined separate variable like below example.

Example

char student_name[10], address[20];
int roll_no[5], class[5];

If we use Structure then we use single variable for all data like belowe example.

Example

struct stu
{
  char student_name[10];
  char address[20];
  int roll_no[5];
  int class[5];
};

Note: Minimum size of Structure is one byte and Maximum size of Structure is sum of all members variable size.

Note: Empty Structure is not possible in C++ Language.

Defining a Structure

Syntax

struct tagname
{
  Datatype1 member1;
  Datatype2 member2;
  Datatype3 member3;
  .........
};

At end of the structure creation (;) must be required because it indicates that an entity is constructed.

Example

struct emp
{
  int id;
  char name[36];
  int sal;
};
sizeof(struct emp)	// --> 40 byte (2byte+36byte+2byte)

Syntax to create structure variable

struct tagname variable;

Difference Between Array and Structure

ArrayStructure
Array is collection of homogeneous data.Structure is the collection of heterogeneous data.
Array data are access using index.Structure elements are access using . (dot) operator.
Array allocates static memory.Structures allocate dynamic memory.
To access Array element need less time compare to structures.To access Structure elements takes more time compare to Array.

Some Important Points Regarding Structure in C++

  • Struct keyword is used to declare structure.
  • Members of structure are enclosed within opening and closing braces { }.
  • Declaration of Structure reserves no space.
  • It is nothing but the "Template / Map / Shape" of the structure .
  • Memory is created, very first time when the variable is created or Instance is created.

Different Ways of Declaring Structure Variable

Declare Structure Variable Immediately after Structure Template in C++

	
struct student
{
  int roll_no;
  char name[20];
}stu;  

// 'stu' is name of Structure variable

Declare Structure Variables using struct Keyword in C++

	
struct student
{
  int roll_no;
  char name[20];
};

struct student stu; // here stu is name of variable

Note: Where "student" is name of structure and stu is name of variable.

Declaring Multiple Structure Variables in C++

	
struct Books 
{
  int pages;
  char name[20];
  char  author[50];
  char  subject[100];
   
}book1,book2,book3;

Note: We can declare multiple variables separated by comma directly after closing curly }.

Syntax to access structure members

By using following operators we can access structure members.

Syntax

.     struct to member
-->   pointer to member

Accessing Structure Members

To access any member of a structure, we use the member access operator (.). The member access operator is coded as a period between the structure variable name and the structure member that we wish to access.

Example of Structure in C++

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

struct emp
{
  int id;
  char name[36];
  float sal;
};

void main()
{
struct emp e; // create structure variable, Here e is variable
clrscr();
cout<<"Enter employee Id, Name, Salary: ";
cin>>e.id;
cin>>e.name;
cin>>e.sal;

cout<<"Id: "<<e.id<<endl;
cout<<"Name: "<<e.name<<endl;
cout<<"Salary: "<<e.sal;
getch();
}

Output

Output:
Enter employee Id, Name, Salary:
5
Gaurav
42600
Id : 05
Name: Gaurav
Salary: 42600.00

When the variable is normal type then go for struct to member operator.

When the variable is pointer type then go for pointer to member operator.

Pass Structure to a Function in C++

A structure variable can be passed to the function as an argument as a normal variable. If structure is passed by value, changes made to the structure variable inside the function definition does not reflect in the originally passed structure variable.

Pass Structure in Function in C++

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

struct student
{
  char name[20];
  int roll;
};

void display(struct student stu);

void main()
{
  struct student stud;
  cout<<"Enter student's name: ";
  cin>>stud.name;
  cout<<"Enter roll number:";
  cin>>stud.roll;
  display(stud);   // passing structure variable stud as argument
  getch();
}
void display(struct student stu)
{
  cout<<"Name: "<<stu.name<<endl;
  cout<<"Roll: "<<stu.roll;
}

Output

Enter student's name: Hitesh Porter
Enter roll number: 18
Name: Hitesh Porter
Roll: 18

Prev Tutorial Next Tutorial

Google Advertisment

Buy This Ad Space @$20 per Month, Ad Size 600X200 Contact on: hitesh.xc@gmail.com or 9999595223

Magenet is best Adsense Alternative here we earn $2 for single link, Here we get links ads. Magenet

For Projects 9999595223

Google Advertisements


Buy Websites 9999595223

Buy College Projects with Documentation Contact on whatsapp 9999595223. Contact on: hitesh.xc@gmail.com or 9999595223 Try this Keyword C++ Programs

Advertisements