Structure in C Language Example
As We have learned Array with Example in our previous articles. Array is a collection of variables with similar data type. Structure is a collection of variables with different data type. Array is derived data type. Structure is User Defined Data Type.
#include<stdio.h>
#include<conio.h>
struct Student
{
int rollno;
char name[12];
};
void main()
{
struct Student S;
struct Student S1={1,"Smit"};
clrscr();
printf("\nEnter Roll No and Name :");
scanf("%d %s",&S.rollno, &S.name);
printf("\nRoll No :%d Name %s",S.rollno,S.name);
getch();
}
/*
Student is name of Structure
rollno and name are member of Structure Student
S is variable of Structure Student
*/
In above example Student is name of Structure , S is variable of Structure and rollno,name are members of Structure. We can access structure members using Variable.membername ex. S.rollno .
Note : We can not assign value at declaration to structure member like int rollno=1; . But we can assign initial values like : struct Student S1={1,”Smit”};