A structure in C is a user-defined data type that allows us to group different types of data under one name. Structures are useful when we need to represent a real-world object such as a student, employee, product, book, or customer.
A structure is a user-defined data type that groups related variables of different data types together.
struct Student
{
int id;
char name[50];
float marks;
};
Here, Student contains an integer, a character array, and a
floating-point value.
Structures are useful for representing records containing related information.
The general syntax for defining a structure is:
struct StructureName
{
data_type member1;
data_type member2;
data_type member3;
};
The semicolon after the closing brace is required.
struct Student
{
int id;
char name[50];
float marks;
};
This defines a structure type named struct Student.
It does not by itself create a variable named student.
After defining a structure, we can declare variables using the structure type.
struct Student student1;
Here, student1 is a structure variable.
The dot operator . is used to access members of a structure variable.
student1.id = 101;
student1.marks = 85.5;
Here, id and marks are members of student1.
A structure variable can be initialized when it is declared.
struct Student student1 = {
101,
"Rahul",
85.5
};
The values are assigned to the members in the order in which they are declared.
C also supports designated initializers, which allow members to be initialized by name.
struct Student student1 = {
.id = 101,
.name = "Rahul",
.marks = 85.5
};
This can make initialization clearer.
#include <stdio.h>
struct Student
{
int id;
char name[50];
float marks;
};
int main()
{
struct Student student1 = {101, "Rahul", 85.5};
printf("ID: %d\n", student1.id);
printf("Name: %s\n", student1.name);
printf("Marks: %.2f\n", student1.marks);
return 0;
}
Structure members can be filled using scanf() and other input
functions.
struct Student student1;
printf("Enter ID: ");
scanf("%d", &student1.id);
printf("Enter marks: ");
scanf("%f", &student1.marks);
A character array inside a structure can be read using fgets().
printf("Enter name: ");
fgets(student1.name, sizeof(student1.name), stdin);
When using fgets(), remember that it may store the newline if
the input fits in the buffer.
A structure can contain many different data types.
struct Employee
{
int id;
char name[50];
float salary;
char grade;
};
This allows related employee information to be stored together.
Multiple variables of the same structure type can be declared.
struct Student student1;
struct Student student2;
struct Student student3;
Each variable has its own copy of the structure members.
Two structure variables of the same structure type can be assigned to each other.
struct Student student1 = {101, "Rahul", 85.5};
struct Student student2;
student2 = student1;
The members of student1 are copied to student2.
An array can contain multiple structure variables.
struct Student students[3];
Each element of the array is a complete struct Student object.
students[0].id = 101;
students[1].id = 102;
students[2].id = 103;
for(int i = 0; i < 3; i++)
{
printf("ID: %d\n", students[i].id);
printf("Name: %s\n", students[i].name);
printf("Marks: %.2f\n", students[i].marks);
}
A loop can process every structure in the array.
A structure can contain another structure as a member.
struct Date
{
int day;
int month;
int year;
};
struct Student
{
int id;
char name[50];
struct Date admissionDate;
};
Use multiple dot operators to access a nested structure member.
student1.admissionDate.day = 15;
student1.admissionDate.month = 8;
student1.admissionDate.year = 2026;
A pointer can store the address of a structure variable.
struct Student student1;
struct Student *ptr = &student1;
The pointer can be used to access the structure.
The arrow operator -> is used to access structure members
through a pointer.
ptr->id = 101;
ptr->marks = 90.5;
The expression ptr->id is equivalent to
(*ptr).id.
A structure can be passed to a function by value.
void display(struct Student s)
{
printf("ID: %d\n", s.id);
printf("Name: %s\n", s.name);
printf("Marks: %.2f\n", s.marks);
}
The function receives a copy of the structure.
A pointer to a structure can be passed to a function.
void display(struct Student *s)
{
printf("ID: %d\n", s->id);
printf("Name: %s\n", s->name);
printf("Marks: %.2f\n", s->marks);
}
This avoids copying the whole structure and allows the function to modify the original structure when the pointer is not const.
void updateMarks(struct Student *s)
{
s->marks = 95.0;
}
int main()
{
struct Student student1 = {101, "Rahul", 85.0};
updateMarks(&student1);
printf("%.2f", student1.marks);
return 0;
}
The typedef keyword can create a shorter name for a structure type.
typedef struct
{
int id;
char name[50];
float marks;
} Student;
Now we can declare:
Student student1;
instead of writing struct Student.
A function can receive a pointer to a constant structure when it only needs to read the structure.
void display(const struct Student *s)
{
printf("%d\n", s->id);
printf("%s\n", s->name);
printf("%.2f\n", s->marks);
}
The function cannot modify the structure through s.
A structure definition must end with a semicolon.
struct Student
{
int id;
char name[50];
};
Forgetting the final semicolon causes a compilation error.
Use the dot operator with a structure variable and the arrow operator with a pointer to a structure.
struct Student student1;
struct Student *ptr = &student1;
student1.id = 101;
ptr->id = 102;
Using the wrong operator can result in a compilation error.
#include <stdio.h>
struct Student
{
int id;
char name[50];
float marks;
};
void display(struct Student *s)
{
printf("\nStudent Details\n");
printf("ID: %d\n", s->id);
printf("Name: %s\n", s->name);
printf("Marks: %.2f\n", s->marks);
}
int main()
{
struct Student student1;
printf("Enter ID: ");
scanf("%d", &student1.id);
printf("Enter name: ");
scanf("%49s", student1.name);
printf("Enter marks: ");
scanf("%f", &student1.marks);
display(&student1);
return 0;
}
This program creates a structure, takes input, and passes the structure to a function using a pointer.
Structures are widely used for organizing related information.
Create a C program that:
Employee structure.
Try using both the dot operator . and arrow operator
->.
. operator accesses members through a structure variable.-> operator accesses members through a structure pointer.typedef can provide a shorter name for a structure type.Question: Which operator is used to access a structure member through a structure pointer?