A structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to its parts as members of that variable by using the dot (.) operator. The power of structures lies in the fact that once defined, the structure name becomes a user-defined data type and may be used the same way as other built-in data types, such as int, double, char.
struct Student
{
int rollno, age;
char name[80];
float marks;
};
int main()
{
/* declare two variables of the new type */
struct Student s1, s3;
/* accessing of data members */
scanf("%d%d%s%f", &s1.rollno, &s1.age, s1.name, &s1.marks);
printf("%d %d %s %f", s1.rollno, s1.age, s1.name, s1.marks);
/* initialization of structure variable */
struct Student s2 = {100, 17, "Aniket", 92};
printf("%d %d %s %f", s2.rollno, s2.age, s2.name, s2.marks);
/* structure variable in assignment statement */
s3 = s2;
printf("%d %d %s %f", s3.rollno, s3.age, s3.name, s3.marks);
return 0;
}
When dealing with the students in a school, many variables of different types are needed. It may be necessary to keep track of name, age, Rollno, and marks point for example.
struct Student
{
int rollno, age;
char name[80];
float marks;
};
Student is called the structure tag, and is your brand new data type, like int, double or char.The most efficient method of dealing with structure variables is to define the structure globally. This tells "the whole world", namely main and any functions in the program, that a new data type exists. To declare a structure globally, place it BEFORE void main(). The structure variables can then be defined locally in main, for example…
Declaring global variables of type struct:
struct Student
{
int rollno, age;
char name[80];
float marks;
} s1, s3;
Declaring structure variables inside main of type struct:
struct Student
{
int rollno, age;
char name[80];
float marks;
};
int main()
{
/* declare two variables of the new type */
struct Student s1, s3;
………
………
return 0;
}
The accessing of data members is done by using the following format:
structure variable.member name
for example
scanf("%d%d%s%f", &s1.rollno, &s1.age, s1.name, &s1.marks);
Initialization is done at the time of declaration of a variable. For example
struct Student s2 = {100, 17, "Aniket", 92};
Structure variable in assignment statement
s3 = s2;
It is possible to use a structure to define another structure. This is called nesting of structure.
Consider the following program
struct Day
{
int month, date, year;
};
struct Student
{
int rollno, age;
char name[80];
struct Day date_of_birth;
float marks;
};
Accessing Member variables of Student
To access members of date_of_birth we can write the statements as below :
struct Student s; // Structure variable of Student
s.date_of_birth.month = 11;
s.date_of_birth.date = 5;
s.date_of_birth.year = 1999;
To store data of 20 students we would be required to use 20 different structure variables from s1 to s20, which is definitely not very convenient. A better approach would be to use an array of structures. Following program shows how to use an array of structures.
#include <stdio.h>
struct student
{
int rollno;
char name[80];
int marks;
};
void accept(struct student[], int);
void display(struct student[], int);
int main()
{
struct student data[20];
int n, choice, rollno;
printf("Number of records you want to enter? : ");
scanf("%d", &n);
accept(data, n);
display(data, n);
return 0;
}
void accept(struct student list[80], int s)
{
int i;
for (i = 0; i < s; i++)
{
printf("\nEnter data for Record #%d", i + 1);
printf("\nEnter rollno : ");
scanf("%d", &list[i].rollno);
fflush(stdin);
printf("Enter name : ");
gets(list[i].name);
printf("Enter marks : ");
scanf("%d", &list[i].marks);
}
}
void display(struct student list[80], int s)
{
int i;
printf("\n\nRollno\tName\tMarks\n");
for (i = 0; i < s; i++)
{
printf("%d\t%s\t%d\n", list[i].rollno, list[i].name, list[i].marks);
}
}
Number of records you want to enter? : 3
Enter data for Record #1
Enter rollno : 100
Enter name : Krishna
Enter marks : 95
Enter data for Record #2
Enter rollno : 101
Enter name : Alex
Enter marks : 85
Enter data for Record #3
Enter rollno : 102
Enter name : Javed
Enter marks : 90
Rollno Name Marks
100 Krishna 95
101 Alex 85
102 Javed 90