C Structures

What are Structures in C?

A structure in C is a user-defined data type that allows you to group different types of data under a single name.

Unlike arrays, which store elements of the same data type, structures can store multiple data types such as integers, characters, and floating-point values together.

Structures are widely used to represent real-world entities like students, employees, books, and products.

Example

struct Student
{
int id;
char name[20];
float marks;
};

Explanation

• Student is the name of the structure.

• id stores the student’s ID.

• name stores the student’s name.

• marks stores the student’s marks.

• A structure groups different data types into one unit.

Why Structures are Important?

Structures are important because they:

• Store different types of data in a single unit.

• Represent real-world objects efficiently.

• Improve code organization and readability.

• Make programs easier to maintain.

• Are widely used in file handling and data structures.

Types of Structures in C

Structures in C can be used in different ways depending on the application.

The most commonly used types are:

• Nested Structure

• Array of Structures

• Self-Referential Structure

Each type helps organize complex data more effectively.

Nested Structure

A nested structure is a structure that contains another structure as one of its members.

It helps organize related information into a hierarchical format.

Example

struct Address
{
char city[20];
};

struct Student
{
int id;
struct Address addr;
};

Explanation

• Address is a separate structure.

• Student contains Address as a member.

• This is called a Nested Structure.

• It helps group related information efficiently.

Array of Structures

An array of structures is a collection of structure variables stored in an array.

It allows you to store information about multiple objects of the same type, such as students, employees, or products.

Example

struct Student
{
int id;
char name[20];
};

struct Student s[3];

Explanation

• Student is a structure.

• s is an array of Student structures.

• It can store information for 3 students.

• Each element of the array is a complete structure.

Self-Referential Structure

A self-referential structure is a structure that contains a pointer to another structure of the same type.

It is commonly used to create linked lists, trees, and other dynamic data structures.

Example

struct Node
{
int data;
struct Node *next;
};

Explanation

• Node is a structure.

• data stores the value.

• next is a pointer to another Node structure.

• Self-referential structures are mainly used in linked lists.

Advantages of Structures

• Store different data types in a single unit.

• Improve program organization.

• Represent real-world entities effectively.

• Reduce code complexity.

• Widely used in file handling and data structures.

Example Program

Program

#include <stdio.h>

struct Student
{
int id;
char name[20];
float marks;
};

int main()
{
struct Student s = {101, “Rahul”, 89.5};

printf(“ID: %d\n”, s.id);
printf(“Name: %s\n”, s.name);
printf(“Marks: %.1f\n”, s.marks);

return 0;
}

Output

ID: 101
Name: Rahul
Marks: 89.5

Explanation

• Student is a user-defined structure.

• s is a structure variable.

• The structure is initialized with ID, Name, and Marks.

• The dot (.) operator is used to access structure members.

• printf() displays each member of the structure.

Key Points

• A structure is a user-defined data type.

• Structures can store different data types together.

• Nested structures improve data organization.

• Arrays of structures store multiple records.

• Self-referential structures are used in linked lists.

• The dot (.) operator accesses structure members.

• Structures are widely used in real-world applications.