3. INTRODUCTION OF STRUCTURE: -
Structure is the collection of variables of different types under a single name
for better handling.
For example:
You want to store the information about person about his/her name,
citizenship number and salary. You can create these information separately but,
approach will be collection of these information under single name because all these
information are related to person.
4. STRUCTURE DEFINE IN C
A structure is a collection of multiple data types that can be referenced with single
name.it may contain similar or different types.
The data items in a structure are called structure elements, members or fields.
5. Uses of Structures
The structure are used to defined new data types.
The users can defined a new data type that may contain different types of data .
A simple variable can store only one value at a time. But a structure variable can store
values at the time.
Difference between array and structure:
The difference between an array and structure is that array consists of a set of
variables of same data type . However, a structure may consist of different data type.
6. DECLARING A STRUCTURE
A structure is declared by using the keyword struct followed by the structure name.
The structure members are defined with their type inside the opening and closing
braces { }.
The closing braces is ended with a semicolon.
The Declaration tells the compiler about the details of the structure .the complier
does not allocate any memory.
The structure is also called “Structure specifier”
7. Syntax of declaring a structure :-
Struct struct_name
{
Data_Type1 identifier1;
Data_type2 identifier2;
:
:
};
8. Example of declares of structure :
Struct student
{
int Rollno, Marks;
float Average;
char Grade;
};
The above structure contains four member variables. The above declaration has
created a new data type student.
A variable of type student store four values at one time.
9. STRUCTURE VARIABLE
The structure variable can be defined after the declaration of a structure.
Structure variable is same as defined a variable of basic types such as Int and Char.
10. Syntax of variable structure
Struct_Name identifier;
Struct_Name is the name of the structure
Identifier is the name of variable to be defined
11. Example:
#include<iostream>
using namespace std;
struct student
{
int rollno;
string name;
string cls;
};
int main()
{
student x;
cout<<"Enter your rollno.";
cin>>x.rollno;
cout<<"Enter your name.";
cin>>x.name;
cout<<"Enter your cls.";
cin>>x.cls;
cout<<x.rollno" "<<x.name" "<<x.cls;
}