Dynamic Structures
Why allocate structures dynamically?
Normally you declare a structure as a fixed variable. But sometimes you do not know how many you will need until the program runs — say, records for however many students the user enters. Dynamic allocation lets you create structures at run time with malloc, using exactly as much memory as you need.
The tools are the same as any dynamic memory: malloc to reserve, free to release — plus one new piece of syntax, the arrow operator.
Allocating a single struct
#include <stdio.h>
#include <stdlib.h>
struct Student { char name[20]; int marks; };
int main() {
struct Student *p = malloc(sizeof(struct Student));
if (p == NULL) return 1; // always check
p->marks = 95; // arrow operator
printf("Marks = %d\n", p->marks);
free(p);
return 0;
}Marks = 95
The arrow operator
When you have a struct variable you use a dot. When you have a struct pointer you use the arrow ->.
| You have | Access with | Example |
|---|---|---|
| A struct variable | dot . | s.marks |
| A pointer to a struct | arrow -> | p->marks |
p->marks is simply a cleaner way to write (*p).marks — first follow the pointer, then reach the member.
A dynamic array of structures
Ask for space for n structures in one malloc, then use normal indexing.
#include <stdio.h>
#include <stdlib.h>
struct Student { char name[20]; int marks; };
int main() {
int n = 3;
struct Student *arr = malloc(n * sizeof(struct Student));
if (arr == NULL) return 1;
for (int i = 0; i < n; i++)
arr[i].marks = (i + 1) * 10; // dot, because arr[i] is a value
for (int i = 0; i < n; i++)
printf("Student %d: %d\n", i + 1, arr[i].marks);
free(arr);
return 0;
}Student 1: 10
Student 2: 20
Student 3: 30
With a whole array pointer you write arr[i].marks — because arr[i] is a struct value, not a pointer. The arrow is only for a plain struct pointer like p->marks.
Freeing the memory
Whether you allocated one struct or an array, one free on the pointer releases the whole block.
free(arr); // releases all n structures arr = NULL; // good habit: avoid a dangling pointer
Common mistakes
- Using dot
.on a pointer (or arrow on a plain variable). - Forgetting to check
mallocfor NULL before use. - Forgetting
free, causing a memory leak. - Using the memory after freeing it (a dangling pointer).
Read a number n, allocate an array of n Student structures, fill each with a name and marks, print them, then free the array. Confirm it works for different values of n.
Summary
- Use
malloc(sizeof(struct ...))to create a structure at run time. - Access members through a pointer with the arrow operator
->. - For
nstructs,malloc(n * sizeof(struct ...))and index witharr[i]. - Use
.on a struct value,->on a struct pointer. - Always check
mallocandfreewhat you allocate.
Frequently Asked Questions
How do you dynamically allocate a structure in C?
malloc with the size of the struct and store the result in a struct pointer, for example struct Student *p = malloc(sizeof(struct Student));. This reserves memory on the heap for one structure, which you then access through the pointer using the arrow operator.What is the arrow operator in C?
-> accesses a member of a structure through a pointer. When p is a pointer to a struct, p->name means "the name member of the struct that p points to." It is shorthand for (*p).name.How do you create a dynamic array of structures in C?
malloc with the count times the struct size, like malloc(n * sizeof(struct Student)), and store it in a struct pointer. You then use normal array indexing such as p[i].marks, and the size can be decided at run time instead of being fixed.What is the difference between the dot and arrow operators?
. accesses a member on a struct variable directly, while the arrow -> accesses a member through a struct pointer. Use . with a struct value and -> with a pointer to a struct.Do you need to free dynamically allocated structures?
malloc must be released with free when you are done, whether it holds a single struct or an array of them. Forgetting to free causes a memory leak that grows the longer the program runs.