Friday, 28 October 2016
Structure in c++:-
Structure:-
Structure is a collection variables of different data items under a common name .Some can be int, some can be float, some can be char and so on. In this case the data item of structure is called member of that structure.
The difference between array and structure is that array is a collection of same type elements that are referred by a common name but in case of structure element of structure can be of different type.
Declaration of Structure:-
Syntax:-
struct structure name
{
data- type 1 member1;
data- type 1 member2;
data- type 1 member3;
-------------------
-------------------
data-type n member n;
};
example:-
struct student
{
char name[10];
int rollno;
char branch[10];
int sem;
};
Creating structure variable:-
If you need to access the data member of Structure , so you have to declare a structure variable. That can access data member of structure. There are various ways to access these member functions.We can create more then one structure variable.
I) We can create structure variable at the time of declaration. Structure variable(s) can be declared after defining the structure type.
struct structure name
{
data- type 1 member1;
data- type 1 member2;
data- type 1 member3;
-------------------
-------------------
data-type n member n;
} structure variable(s);
Example:-
struct student
{
char name[10];
int rollno;
char branch[10];
int sem;
}var ;
Program:-
II) We can also create a structure variable with in a main function.
Example:-
struct student
{
char name[10];
int rollno;
char branch[10];
int sem;
} ;
main()
{
struct student var;
----
-------
}
Program:-
Accessing Structure Elements:-
To access member of structure we have to use the . (dot) operator.
syntax:-
structure_variable_name.member
- If you need to read a statement from keyboard then we have to write down the following statement:-
cin>>var.rollno;
In which var is a structure variable and rollno is a member of structure as we declare above. Remember the .(dot)operator between structure variable and member of structure.
- If you need to print a statement then write down the following statement :-
cout<<var.rollno;
In which you can see that it will print rollno that you entered before.
Structure is a collection variables of different data items under a common name .Some can be int, some can be float, some can be char and so on. In this case the data item of structure is called member of that structure.
The difference between array and structure is that array is a collection of same type elements that are referred by a common name but in case of structure element of structure can be of different type.
Declaration of Structure:-
Syntax:-
struct structure name
{
data- type 1 member1;
data- type 1 member2;
data- type 1 member3;
-------------------
-------------------
data-type n member n;
};
example:-
struct student
{
char name[10];
int rollno;
char branch[10];
int sem;
};
Creating structure variable:-
If you need to access the data member of Structure , so you have to declare a structure variable. That can access data member of structure. There are various ways to access these member functions.We can create more then one structure variable.
I) We can create structure variable at the time of declaration. Structure variable(s) can be declared after defining the structure type.
struct structure name
{
data- type 1 member1;
data- type 1 member2;
data- type 1 member3;
-------------------
-------------------
data-type n member n;
} structure variable(s);
Example:-
struct student
{
char name[10];
int rollno;
char branch[10];
int sem;
}var ;
Program:-
Write a program in c++ to read and write in structure
II) We can also create a structure variable with in a main function.
Example:-
struct student
{
char name[10];
int rollno;
char branch[10];
int sem;
} ;
main()
{
struct student var;
----
-------
}
Program:-
Write a program in c++ to read and write in structure
Accessing Structure Elements:-
To access member of structure we have to use the . (dot) operator.
syntax:-
structure_variable_name.member
- If you need to read a statement from keyboard then we have to write down the following statement:-
cin>>var.rollno;
In which var is a structure variable and rollno is a member of structure as we declare above. Remember the .(dot)operator between structure variable and member of structure.
- If you need to print a statement then write down the following statement :-
cout<<var.rollno;
In which you can see that it will print rollno that you entered before.
Subscribe to:
Posts (Atom)