Structures in C
Subject: Data Structures and Algorithms · Language: C · Level: beginner · 6 min read
Group related fields using struct and model real-world or DSA records.
A structure is a user-defined data type that groups related variables under one name. It is useful when one record has many details.
For example, a student record may have roll number, name, marks, and grade. These values have different data types, so a normal array is not the right choice. A structure handles this cleanly.
Why Structures Are Important
Structures help us model real-world data in C. They are also used heavily in DSA to create nodes for linked lists, stacks, queues, trees, and graphs.
Real-Life Meaning
Think of a structure like a form. A student form may contain roll number, name, marks, branch, and grade. These fields belong to one student, so keeping them inside one structure makes the code organized.
Without structures, you may create separate arrays like `rollNo[]`, `name[]`, and `marks[]`. That becomes difficult to manage because one student's data is spread across different places.
Defining a Structure
```c
struct Student {
int rollNo;
char name[30];
float marks;
};
```
This creates a new structure type named `Student`. It does not create a student variable yet; it only defines the design.
Members of a Structure
The variables inside a structure are called members. In `struct Student`, `rollNo`, `name`, and `marks` are members.
Each member can have a different data type. This is the main reason structures are useful.
Creating a Structure Variable
```c
struct Student s1;
```
Now `s1` can store one student's roll number, name, and marks.
Initializing a Structure
A structure variable can be initialized when it is created.
```c
struct Student s1 = {101, "Aman", 88.5f};
```
The values are assigned in the same order as the members are written inside the structure definition.
Accessing Members
Use the dot operator `.` to access structure members.
```c
s1.rollNo = 101;
```
```c
#include <stdio.h>
struct Student {
int rollNo;
char name[30];
float marks;
char grade;
};
int main(void) {
struct Student s1 = {101, "Aman", 88.5f, 'A'};
printf("Roll No: %d\n", s1.rollNo);
printf("Name: %s\n", s1.name);
printf("Marks: %.2f\n", s1.marks);
printf("Grade: %c\n", s1.grade);
return 0;
}
```
Array of Structures
When you need to store many records, use an array of structures.
```c
struct Student students[50];
```
This can store 50 student records, where each record has roll number, name, and marks.
You can access one student's marks like this:
```c
students[0].marks
```
This means marks of the first student.
Structure vs Array
- An array stores many values of the same type
- A structure stores related values of different types
- Use an array for a list of marks
- Use a structure for a complete student record
Nested Structure
A structure can contain another structure. This is called a nested structure.
```c
struct Date {
int day;
int month;
int year;
};
struct Student {
int rollNo;
struct Date admissionDate;
};
```
Nested structures are useful when one record has another meaningful sub-record inside it.
typedef with Structure
`typedef` can make structure names shorter and easier to use.
```c
typedef struct Student Student;
```
After this, you can write `Student s1;` instead of `struct Student s1;`.
Structure with Pointer
When a structure is accessed through a pointer, use the arrow operator `->`.
```c
studentPtr->marks
```
This is common in linked lists because each node is usually accessed through a pointer.
Structures in DSA
Structures are the base of many data structures in C.
- Linked list nodes use structures
- Tree nodes use structures
- Graph adjacency nodes can use structures
- Stack and queue records can use structures
- Student, employee, product, and account records can use structures
Important Points
- Use structures when one entity has multiple related fields
- Use the dot operator with normal structure variables
- Use the arrow operator with structure pointers
- Use arrays of structures for multiple records
- Structures make large programs easier to organize
Practice Question
Create a structure named `Employee` with id, name, salary, and department. Store three employee records and print the employee with the highest salary.