Structures & Unions in C
A structure (struct) in C groups several related variables of different types under one name, so you can treat them as a single unit. A union looks similar but stores only one of its members at a time, sharing the same memory. Structures are everywhere in real C code — they model real-world things like a student, a point, or a date.
Defining a structure
struct Student {
char name[30];
int roll;
float marks;
}; /* don't forget the semicolon after the closing brace */
This defines a type called struct Student. Now you can create variables of that type and fill in each member:
#include <stdio.h>
struct Student {
char name[30];
int roll;
float marks;
};
int main(void) {
struct Student s1 = {"Aarav", 12, 88.5f}; /* initialize members in order */
/* Access members with the dot operator */
printf("%s (roll %d) scored %.1f\n", s1.name, s1.roll, s1.marks);
s1.marks = 91.0f; /* update a single member */
return 0;
}
The dot and arrow operators
- Use
.(dot) when you have a struct variable:s1.roll. - Use
->(arrow) when you have a pointer to a struct:ptr->roll. It's shorthand for(*ptr).roll.
struct Student s1 = {"Mira", 7, 76.0f};
struct Student *p = &s1; /* pointer to the struct */
printf("%d\n", p->roll); /* arrow: same as (*p).roll -> 7 */
This connects directly to pointers in C.
typedef for cleaner names
Writing struct Student everywhere is verbose. typedef gives the type a short alias:
typedef struct {
int x;
int y;
} Point; /* now "Point" is the type name */
Point origin = {0, 0}; /* no "struct" keyword needed */
Want to learn this properly?
Join the waitlist for C Programming — beginner-friendly, project-first classes in Jalgaon.
Explore C ProgrammingUnions: shared memory
A union has members like a struct, but they all share the same memory location, so it can hold only one member's value at a time. Its size is that of its largest member:
#include <stdio.h>
union Value {
int i;
float f;
char c;
}; /* all three overlap in memory */
int main(void) {
union Value v;
v.i = 65; /* store an int */
printf("%d\n", v.i); /* 65 */
v.f = 3.14f; /* now the SAME memory holds a float */
/* reading v.i now is meaningless -- it was overwritten */
printf("%.2f\n", v.f); /* 3.14 */
return 0;
}
Use a union when a value can be one of several types at different times and you want to save memory. Use a struct when you need all the fields at once.
struct vs union at a glance
- struct: members each get their own memory; total size is the sum (plus padding). Holds all fields together.
- union: members share memory; total size is the largest member. Holds one field at a time.
Common mistakes
- Forgetting the semicolon after the closing
}of a struct/union definition. - Using
.on a pointer (or->on a plain variable): match the operator to whether you have a pointer. - Reading a union member you didn't last write — you get garbage, because the memory was overwritten.
- Comparing structs with
==— not allowed; compare members individually. - Assuming a struct's size equals the sum of members — the compiler may add padding for alignment.
FAQ
Can a struct contain another struct? Yes — nesting is common, e.g. a Student containing a Date dob.
When would I use a union? Rarely as a beginner, but it's useful for memory-constrained code or type-tagged values.
Continue at the hub C Programming, then Pointers in C and Dynamic Memory in C.
Want to learn this properly? Join the waitlist for our C Programming course — taught in Jalgaon.
Want to learn this properly?
Join the waitlist for C Programming — beginner-friendly, project-first classes in Jalgaon.
Explore C ProgrammingFounder, Infoplanet
Atul Kabra founded Infoplanet in 2001 and has spent over two decades teaching programming — C, C++, Java, databases and more — to students across Maharashtra.
Related guides
Arrays in C
A beginner guide to arrays in C — declaring, initializing and indexing them, looping over elements, multidimensional arrays, and avoiding out-of-bounds errors.
Common C Errors & How to Fix Them
A troubleshooting guide to the most common C errors beginners face — from = vs ==, format mismatches and missing semicolons to segmentation faults — and how to fix each.
Dynamic Memory Allocation in C
A beginner guide to dynamic memory allocation in C — malloc, calloc, realloc and free, allocating at runtime, and preventing memory leaks.
