Structure in C
What is Structure in C?
Structure is user defined or derived data type which can store different data types.It store the values in continuous memory locations like an array.
Syntax to declare Structure
struct structureName { dataType member1; dataType member2; dataType member3; ... };
We can declare structure with keyword struct.Here, member1,member2,member3 are the data member of a structure.
Create variable of a structure
struct structureName { dataType member1; dataType member2; dataType member3; ... }variable_name;
Another way of creating variable of structure
struct structureName
{
dataType member1;
dataType member2;
dataType member3;
...
};
void main()
{
struct structureName variable_name;
}
Operators used to access the data members of a Structure
-> -This is arrow or indirection operator used to access the data members of a structure using pointer.
Example:
#include<stdio.h>
#include<conio.h>
struct student
{
int rollno;
float fee;
char name[10];
}st;
void main()
{
//struct student st;
printf("Enter rollno");
scanf("%d",&st.rollno);
printf("Enter fee");
scanf("%f",&st.fee);
printf("Enter name");
scanf("%s",&st.name);
printf("\nRollno is %d",st.rollno);
printf("\nFee is %f",st.fee);
printf("\nName is %s",st.name);
getch();
}//end of main
Output
Enter rollno 10
Enter fee 5000
Enter name compuhelp
Rollno is 10
Fee is 5000.000000
Name is compuhelp