Introduction to Structure in C
Structure is a user defined data of C language. It is a collection of different values of same or different data types. It is basically used to group logically related data.
In C language, syntax for defining a structure is:
struct <Struct_name>
{
<datatype-1> var1;
<datatype-2> var2;
:
:
<datatype-N> varN;
};
Here, struct is the keyword to start creating a structure in C.
Struct_name is the valid name of structure.
datatype-1, data-type2,… data-typeN are data types of different structure members.
var1, var2,… varN are variables declared inside a structure. They are called structure members.
; Semicolon is needed to end a structure.
Example
struct data
{
int a;
float b;
};
In the above example, structure named data has been defined which contains two elements named a and b.
Declaration of a Structure variable in C
We can declare variables of a structure just as we declare variables of other data types.
Syntax:
struct <Struct_name> v1,v2,…..vN;
Here, Struct_name is the name of an existing structure .
v1,v2,……..vN are names of structure variables.
Example:
struct data d;
In the above example, variable d has been declared for already declared structure data.
Accessing structure members in C
Dot (.) operator or arrow operator -> can be used with the structure variable to work with members of a structure.
The dot operator is required for normal structure variables and arrow operator is required for pointer variables of structure.
The syntax for referring to structure member is:
struct_var.member;
or
struct_var->member;
Here, struct_var is the name of structure variable and member is a structure element.
Program to demonstrate the use of structure in C |
#include<stdio.h> struct data { int a; float b; }; int main() { struct data d; d.a=13; d.b=20.55; printf("\nd.a=%d",d.a); printf("\nd.b=%f",d.b); return(0); } |
Output |
d.a=13 d.b=20.549999 |
Description |
In the above program, structure variable d contains 13 for its first element a and 20.55 for its second element b. |
Initialization of a structure variable in C
We can use pair of braces to initialize a structure variable.
Syntax:
struct <Struct_name><var>={Values};
Example:
struct data
{
int a;
float b;
};
struct data d={2,5.5};
In this example, structure variable d has been initialized with values 20,50.5. 20 would be stored in d.a and 50.5 would be stored in d.b .
Program to initialize a structure variable in C |
#include<stdio.h> struct data { int a; float b; }; int main() { struct data d={3,7.8}; printf("\nd1.a=%d",d1.a); printf("\nd1.b=%f",d1.b); printf("\nd.a=%d",d.a); printf("\nd.b=%f",d.b); return(0); } |
Output |
d.a=3 d.b=7.800000 |
Description |
In the above program, structure variable d has been initialized to contain 3 for its first element a and 7.8 for its second element b. |
Program to read and display a structure variable in C
|
#include<stdio.h> struct student { char name[25]; int rollno; float fees; }; int main() { struct student s; printf("\nEnter Name of student:="); gets(s.name); printf("\nEnter rollno:="); scanf("%d",&s.rollno); printf("\nEnter fees:="); scanf("%f",&s.fees); printf("\nStudent 's Name=%s",s.name); printf("\nRollno=%d",s.rollno); printf("\nFees=%f",s.fees); return(0); } |
Output |
Enter Name of student:=Amit Enter rollno:=1 Enter fees:=5000 Student 's Name=Amit Rollno=1 Fees=5000.000000 |