🔴 Advanced  ·  Lesson 46

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

C Language
#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;
}
Output:
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 haveAccess withExample
A struct variabledot .s.marks
A pointer to a structarrow ->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.

C Language
#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;
}
Output:
Student 1: 10
Student 2: 20
Student 3: 30
💡 Dot here, arrow there

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.

C Language
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 malloc for NULL before use.
  • Forgetting free, causing a memory leak.
  • Using the memory after freeing it (a dangling pointer).
🏋️ Practice

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 n structs, malloc(n * sizeof(struct ...)) and index with arr[i].
  • Use . on a struct value, -> on a struct pointer.
  • Always check malloc and free what you allocate.

Frequently Asked Questions

How do you dynamically allocate a structure in C?
You use 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?
The arrow operator -> 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?
You call 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?
The dot . 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?
Yes. Every block you get from 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.
← Back to C Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.