What is structure (struct) in c
Structure is a collection of heterogeneous data items treated as a single unit. These data items can be of different types. Each data item is called a member of structure. The keyword struct is used to declare structure. The memory of a structure is determined by the sum of memory allocated to all the members of a structure. In this structure, multiple members can be simultaneously accessed at a time.
As we know that an array is the collection of similar data items such as float or int a single entity. It is not possible to hold different data items with different data types in an array, so structure is the best solution to hold dissimilar data items as a single entity.
The syntax for structure declaration
In this example variable 1, variable 2, variable n are the members of the structure and var is a variable with data type tag_name.
struct structure_ name
{
data type variable 1;
data type variable 2;
……………………………..;
data type variable n;
};
struct structure_name var;
Example of structure variable
In this example record is structure type and int roll_no, char fname[30] and char lname[30] are the members of the structure and a is the structure variable
struct record
{
int roll_no;
char fname[30];
char lname[30];
};
struct record a;
Structure initialization
Structure variable can be initialized quite similar to array. The following example shows the structure initialization process.
sruct student
{
int roll_no;
char fname[30];
char lname[30];
S1={1, “Ram”, “Rai”};
}
The alternative method to initialize structure variable as:
struct student S2={1, “Sita”,”Devi”};
struct student S3={2,”Hari”,”Sharma”};
Accessing structure members
A member of structure can be accessed by using period(.) sign between structure variable and respective member.
Syntax
Variable.member;
Example
S1.roll_no;
Example of structure
Write a C program to take student name, roll no, mark, and prints the record on-screen.
#include<stdio.h>
#include<conio.h>
void main()
{
struct record
{
int roll;
float mark;
char name[20];
};
struct record a;
{
printf("\n Enter Roll number, Name and marks of student");
scanf("%d%s%f", &a.roll, a.name, &a.mark);
printf("\n Roll number=%d\tName=%s\t marks=%f",a.roll, a.name, a.mark);
}
getch();
}
Output
Feature of structure
It can be treated as a record that is a collection of interrelated data fields having different data types.
It Is possible to copy one structure variable to another by simply assignment operator(=).
It allows a nested structure that is one structure inside another structure.
It is possible to define structure pointer also known as linked list.
Let check another example
Write a c program to take Roll no, Name and mark of 5 students and display it.
#include<stdio.h>
#include<conio.h>
void main()
{
struct record
{
int roll;
float mark;
char name[20];
};
int i;
struct record a[5];
for(i=0;i<5;i++)
{
printf("\n Enter Roll number, Name and marks of student");
scanf("%d%s%f",&a[i].roll, a[i].name, &a[i].mark);
}
for(i=0;i<5;i++)
{
printf("\n Roll number=%d\tName=%s\t marks=%f",a[i].roll, a[i].name, a[i].mark);
}
getch();
}
Output
In this example, we have to print a record of 5 students. It is possible to declare structure as an array; called an array of structure. It is also known as the collection of structure variables having the same set of members. To display 5 students' records we have to make the variable a[5]; as an array.
Difference between structure and union in C


0 Comments
Please do not enter any spam link on comment box