Structures in C are used when a program needs to group different types of data under one name. In normal variables, each value is usually stored separately. But many real-world entities contain multiple related values. A student may have a name, roll number, and marks. A point may have x and y coordinates. An employee may have an ID, salary, and department. Structures allow all these related values to be stored together in a meaningful way.
This makes structures one of the most important user-defined data types in C. Once you understand structures, it becomes much easier to work with records, arrays of records, pointers to complex data, files, and many embedded and systems programming tasks. In this article, we will understand structures in C, their syntax, declaration, initialization, member access, memory behavior, pointers, functions, and common mistakes.
What are Structures in C?
A structure in C is a user-defined data type that groups related variables of different types into one unit. Each variable inside a structure is called a member.
Unlike an array, where all elements must be of the same type, a structure can contain different types such as int, float, char, and even other structures.
A structure in C is used to represent one logical entity made of multiple related values, even when those values have different data types.
Why Structures are Needed in C
- They group related data under one name.
- They make programs more organized and readable.
- They allow modeling of real-world records such as students, employees, sensors, or packets.
- They are useful in arrays, functions, file handling, and pointers.
- They reduce the need to manage many separate variables manually.
Without structures, programs that deal with records become messy very quickly because each field must be handled as a separate variable.
Syntax of Structure in C
The basic syntax of a structure definition is:
struct StructureName
{
data_type member1;
data_type member2;
data_type member3;
};| Part | Meaning |
|---|---|
struct | Keyword used to define a structure |
StructureName | Name of the new structure type |
| members | Variables stored inside the structure |
The semicolon after the closing brace is required because the structure definition is a declaration statement.
Declaring and Defining Structure Variables
After defining a structure type, you can create variables of that structure.
#include <stdio.h>
struct Student
{
int roll;
float marks;
char grade;
};
int main(void)
{
struct Student s1;
return 0;
}Here struct Student is the type and s1 is a variable of that type.
Initialization of Structure in C
Structure members can be initialized when the variable is created.
#include <stdio.h>
struct Student
{
int roll;
float marks;
char grade;
};
int main(void)
{
struct Student s1 = {101, 87.5f, 'A'};
return 0;
}The first value goes to roll, the second to marks, and the third to grade.
Another common style is member-wise assignment after declaration.
s1.roll = 101;
s1.marks = 87.5f;
s1.grade = 'A';Accessing Structure Members in C
Structure members are accessed using the dot operator ..
#include <stdio.h>
struct Student
{
int roll;
float marks;
char grade;
};
int main(void)
{
struct Student s1 = {101, 87.5f, 'A'};
printf("Roll = %d\n", s1.roll);
printf("Marks = %.2f\n", s1.marks);
printf("Grade = %c\n", s1.grade);
return 0;
}The dot operator is used when you have a normal structure variable, not a pointer.
Structure and Functions in C
Structures can be passed to functions either by value or by address. Passing by value creates a copy. Passing by address lets the function work with the original structure.
Example of passing structure by value:
#include <stdio.h>
struct Student
{
int roll;
float marks;
};
void display(struct Student s)
{
printf("Roll = %d, Marks = %.2f\n", s.roll, s.marks);
}
int main(void)
{
struct Student s1 = {101, 90.0f};
display(s1);
return 0;
}For larger structures, passing a pointer is often more efficient.
Pointer to Structure in C
A pointer can point to a structure variable. In that case, members are usually accessed with the arrow operator ->.
#include <stdio.h>
struct Student
{
int roll;
float marks;
};
int main(void)
{
struct Student s1 = {101, 88.0f};
struct Student *ptr = &s1;
printf("Roll = %d\n", ptr->roll);
printf("Marks = %.2f\n", ptr->marks);
return 0;
}The expression ptr->roll means the same as (*ptr).roll, but the arrow form is much easier to read.
Array of Structures in C
C allows arrays whose elements are structures. This is useful for storing multiple records of the same type, such as many students or products.
Because this topic deserves a deeper treatment, it is usually studied as a separate article after learning basic structures. For now, the key idea is that each array element becomes one complete structure object.
Difference Between Structure and Array in C
| Point | Structure | Array |
|---|---|---|
| Stored data types | Can store different data types together | Stores only same data type elements |
| Purpose | Represents one logical record | Represents many values of one type |
| Access style | Member names with . or -> | Index positions like arr[i] |
| Example use | Student record | List of marks |
Structures and arrays solve different problems. In practice, they are often used together.
Memory Layout of Structure in C
The total size of a structure is not always the simple sum of the sizes of its members. Many systems add padding bytes between members or at the end of the structure for alignment reasons.
For example, if a structure contains a char followed by an int, the compiler may insert extra unused bytes between them so that the int starts at a properly aligned address.
This is why sizeof(struct Name) should be checked instead of assuming the result manually.
#include <stdio.h>
struct Example
{
char a;
int b;
};
int main(void)
{
printf("Size = %zu\n", sizeof(struct Example));
return 0;
}Padding and alignment become especially important in embedded systems, communication protocols, and memory-sensitive programs.
typedef with Structures in C
Structures are often combined with typedef so that the struct keyword does not have to be written every time.
typedef struct
{
int roll;
float marks;
} Student;
Student s1;This style is common in real projects because it reduces repetition and often makes the code easier to read.
Common Mistakes in Structures in C
- forgetting the semicolon after the structure definition
- using
.on a structure pointer instead of-> - assuming structure size is always the exact sum of member sizes
- confusing arrays and structures
- passing large structures by value when a pointer would be better
| Mistake | Problem | Better Practice |
|---|---|---|
Missing semicolon after struct definition | Compilation error | Always end the definition with ; |
Using ptr.member | Wrong access for pointer | Use ptr->member |
| Ignoring padding | Wrong assumptions about memory size | Check with sizeof |
Best Practices for Structures in C
- Use structures when multiple related values belong to one logical object.
- Choose clear member names that show meaning directly.
- Use pointers for large structures when passing to functions for efficiency.
- Be aware of alignment and padding when memory layout matters.
- Use
typedefif it improves readability in your codebase.
FAQs
What are structures in C?
Structures in C are user-defined data types used to group related variables of different data types under one name.
What is the difference between structure and array in C?
An array stores multiple values of the same type, while a structure stores related values that may have different types.
How do you access members of a structure in C?
Use the dot operator . for normal structure variables and the arrow operator -> for structure pointers.
Can structures contain different data types in C?
Yes. That is one of the main purposes of structures in C.
Why is sizeof(struct) sometimes larger than expected?
Because the compiler may add padding bytes for memory alignment between members or at the end of the structure.
Can a structure be passed to a function in C?
Yes. A structure can be passed either by value or by pointer, depending on what the function needs to do.