struct in c++


in this tutorials we are going to learn structure of c++ program 

you can read our previous C++ tutorial 

types of data in c++

loop in programming

C++ array 

let start today C++ tutorial

struct in c++

structure of c++ program

structure of c++ program

what is structure

Structure is a group of simple variable. Variable may be different kinds in structures like char, int, float , and so on.  Data items in a structures are called as members of structures .

Let start Structure tutorial  with a simple structure that contains three variable  two int and one float . This structure represent a company data .  The structure is a kind of sketch declaring what info is required for a single.

Company makes  several kinds of widgets, so the widget model number is first member of structure. Number of part itself is next member, and final member is part’ cost.


simple structure Program.


using namespace std;

struct  part {                                                //declare a structure

int modelNum;                                                    // ID number of widget;

int  partNum;                                                       //ID number of widget part

float cost;                                                            //cost of part

}  ;

int main() {

part p1 ;                                                          //define a structure variable

//giving values to structure members

p1.modelNum  = 1205;

p1.partNum = 100;

p.cost = 300.50;

//display structure members

cout<<”Model no”<<p1.modelNum;

cout<<”Part no”<<p1.partNum;

cout<<”Part cost”<<p1.cost<<endl;

return 0;



When this program  will run following output will be display on screen:

Model no 1205 , Part no 100 , Part cost 300.50


Let consider this very simple Structure Program.

structure definition

structure definition

structure definition

structure definition tells how c++ struct is organized . it specifies what members structure will have .

struct part {

int modelNum;                                                   

int  partNum;

float cost;                                                           

} ;


structure syntax

Keyword struct introduces structure definition.  Next comes structure name which is part in program example . declarations of structure members




are enclosed in braces. A semicolon follows closing brace, terminating entire structure .


Use of structure definition

Structure definition state functions only as a plan for creation of variable of type part . A structure definition is just a description for how structure variable will look when they are defined .it does not itself create any structure variable . it does not set apart any space in memory . This is unlike definition of a simple variable, which does set apart memory

structure definition

structure definition


How to define  struct in c++ program variable 

First stamen in main ()

part p1;

define a variable , called p1 , of type structure part. This definition reserve space in memory for p1 to hold all members of p1 which are mobelNum , partNum and cost.

How to acces Structure Members

Once a structure variable has been defined, its members can be accessed using  dot operator  (.).

p1.modelNum =   1205;

this structure member is written in three parts :

Name of structure variable (p1)

dot operator , which consists of period (.)

member  name .



in this tutorial we have learnt

what is structure ?

how to define a structure?


if you want know something more about structure , please write use in comment box.


Leave a Reply

Your email address will not be published. Required fields are marked *