C Notes
Complete guide to structures in C programming — definition, declaration, initialization, accessing members with dot operator, struct memory layout, size and padding, practical examples with output.
Imagine you're building a student management system. Each student has a name, roll number, age, and GPA. Without structures, you'd need separate arrays for each field — messy and error-prone. Structures let you group related variables of different types into a single unit, making your code cleaner and more logical.
What is a Structure?
A structure is a user-defined data type that combines multiple variables (called members) of different types under one name.
Think of a structure as a blueprint — like a form template. Each variable of that structure type is like a filled-in form.
Declaring Structure Variables
There are multiple ways to declare structure variables:
Initialization and Access
Student 1: Alice Johnson (Roll: 101, GPA: 3.85) Student 2: Bob Smith (Roll: 102, GPA: 3.65) Student 3: Charlie (Roll: 103, GPA: 3.90)
Memory Layout of a Structure
| roll_no | name[50] | gpa | age |
|---|---|---|---|
| 4 bytes | 50 bytes | 4 bytes | 4 bytes |
| 101 | "Alice\0..." | 3.85 | 20 |
Structure Padding and sizeof
Compilers add padding bytes for alignment:
#include <stdio.h>
struct Padded {
char c; // 1 byte + 3 padding
int i; // 4 bytes
char d; // 1 byte + 3 padding
};
struct Packed {
int i; // 4 bytes
char c; // 1 byte
char d; // 1 byte + 2 padding
};
int main() {
printf("struct Padded: %zu bytes\n", sizeof(struct Padded));
printf("struct Packed: %zu bytes\n", sizeof(struct Packed));
return 0;
}struct Padded: 12 bytes struct Packed: 8 bytes
Lesson: Order members from largest to smallest to minimize padding.
Copying Structures
Unlike arrays, structures can be copied with simple assignment:
#include <stdio.h>
struct Point {
int x, y;
};
int main() {
struct Point p1 = {10, 20};
struct Point p2 = p1; // Copies all members!
p2.x = 99; // Changing p2 does NOT affect p1
printf("p1: (%d, %d)\n", p1.x, p1.y);
printf("p2: (%d, %d)\n", p2.x, p2.y);
return 0;
}p1: (10, 20) p2: (99, 20)
Structures and Functions
#include <stdio.h>
#include <math.h>
struct Point {
double x, y;
};
// Pass by value (receives a copy)
double distance(struct Point a, struct Point b) {
double dx = a.x - b.x;
double dy = a.y - b.y;
return sqrt(dx * dx + dy * dy);
}
// Return a structure
struct Point midpoint(struct Point a, struct Point b) {
struct Point mid;
mid.x = (a.x + b.x) / 2.0;
mid.y = (a.y + b.y) / 2.0;
return mid;
}
int main() {
struct Point p1 = {0.0, 0.0};
struct Point p2 = {6.0, 8.0};
printf("Distance: %.2f\n", distance(p1, p2));
struct Point m = midpoint(p1, p2);
printf("Midpoint: (%.1f, %.1f)\n", m.x, m.y);
return 0;
}Distance: 10.00 Midpoint: (3.0, 4.0)
Structures vs Arrays vs Basic Types
| Feature | Basic Types | Arrays | Structures |
|---|---|---|---|
| Data types | Single type | Same type elements | Mixed types |
| Access | By name | By index | By member name |
| Assignment | Yes | No (must copy manually) | Yes (full copy) |
| Pass to function | By value | By pointer (decays) | By value or pointer |
| Size | Fixed | Fixed | Sum of members + padding |
Practical Example: Library Book System
Title: The C Programming Language Author: Kernighan & Ritchie Price: $45.99 Pages: 272 Published: 01/01/1978
Interview Questions
Q1: Can a structure contain a pointer to itself?
Yes! This is called a self-referential structure and is the foundation of linked lists, trees, and graphs. Example: struct Node { int data; struct Node *next; };
Q2: What is the difference between structure declaration and definition?
Declaration tells the compiler the structure exists (forward declaration): struct Student;. Definition provides the full layout with members. You can declare a pointer to an incomplete type but can't access members until it's defined.
Q3: Why is padding added to structures?
CPUs access memory most efficiently at aligned addresses (e.g., 4-byte int at addresses divisible by 4). Padding ensures each member starts at a properly aligned address, avoiding slow unaligned access or hardware exceptions on some architectures.
Q4: Can you compare two structures with == operator?
No. C doesn't support == for structures because padding bytes may contain garbage. You must compare member by member, or use memcmp() (but memcmp can give wrong results due to padding). Write a custom comparison function.
Summary
- Structures group related variables of different types into one unit
- Access members with the dot (
.) operator - Structures can be assigned, passed to functions, and returned from functions
- Compiler adds padding for alignment — order members by size to save space
- Use designated initializers (C99) for readable initialization
- Structures form the basis for OOP-like patterns in C
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Structures in C – User-Defined Data Types.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this C Programming topic.
Search Terms
c-programming, c programming, programming, structures, and, unions, structures in c – user-defined data types
Related C Programming Topics