< C++ Tutorial

C++ structure

← Previous Next →


Structure is a user defined data type which can hold multiple variable (of different data type).

For example: You want to store information for any person like - name, id, phone.

Syntax :

struct variable-name 
{
    data-type1 var1;
    data-type2 var2;
    data-type3 var3;
};

Example :

struct Person
{ 
    char name[30];
    int id;
    int phone;
};

How to use in program 

#include <iostream>
using namespace std;

struct Person
{ 
    char name[30];
    int id;
    int phone;
};

int main()
{ 
   struct Person p; // create a p variable of data type Person

   p.name = "George"; // . operator is used to access any member of structure
   p.id = 202;
   p.phone = 102345908;

   cout << "Name:" << p.name << "\n" ;
   cout << "Id:" << p.id << "\n" ;
   cout << "Phone:" << p.phone << "\n" ;

   return 0;
}

 

← Previous Next →