Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING BASH RUST

C Tutorial

C HOME C Intro C Get Started C Syntax C Output C Comments C Variables C Data Types C Constants C Operators C Booleans C If...Else C Switch C While Loop C For Loop C Break/Continue C Arrays C Strings C User Input C Memory Address C Pointers

C Functions

C Functions C Function Parameters C Scope C Function Declaration C Recursion C Math Functions

C Files

C Create Files C Write To Files C Read Files

C Structures

C Structures C Structs & Pointers C Unions

C Enums

C Enums

C Memory

C Memory Management

C Errors

C Errors C Debugging C NULL C Error Handling C Input Validation

C More

C Date C Macros C Organize Code C Storage Classes

C Projects

C Projects

C Reference

C Reference C Keywords C <stdio.h> C <stdlib.h> C <string.h> C <math.h> C <ctype.h> C <time.h>

C Examples

C Examples C Real-Life Examples C Exercises C Quiz C Compiler C Syllabus C Study Plan C Certificate

C Structures and Dynamic Memory


Structures and Dynamic Memory

You can also use dynamic memory with structures.

This is useful when you don't know how many structs you'll need in advance, or want to save memory by only allocating what's necessary (e.g., in a car dealership program where the number of cars is not fixed).


Allocating Memory for a Struct

You can use the malloc() function to allocate memory for a struct pointer:

Example

#include <stdio.h>
#include <stdlib.h>
#include <string.h> 

struct Car {
  char brand[50];
  int year;
};

int main() {
  // Allocate memory for one Car struct
  struct Car *ptr = (struct Car*) malloc(sizeof(struct Car));

  // Check if allocation was successful
  if (ptr == NULL) {
    printf("Memory allocation failed.\n");
    return 1; // Exit the program with an error code
  }

  // Set values
  strcpy(ptr->brand, "Honda");
  ptr->year = 2022;

  // Print values
  printf("Brand: %s\n", ptr->brand);
  printf("Year: %d\n", ptr->year);

  // Free memory
  free(ptr);

  return 0;
}

Try it Yourself »

Example Explained

  • malloc() allocates memory for one struct
  • strcpy() is used to copy a string into the brand field
  • We use -> to access members through the pointer
  • free() is used at the end to release the memory

Note: malloc() allocates uninitialized memory. The content will be undefined until you assign values. If you want memory initialized to zero, you can use calloc().


Using Arrays of Structs

You can also allocate memory for multiple structs at once, like an array:

Example: Allocate memory for 3 cars

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct Car {
  char brand[50];
  int year;
};

int main() {
  struct Car *cars = (struct Car*) malloc(3 * sizeof(struct Car));

  if (cars == NULL) {
    printf("Memory allocation failed.\n");
    return 1 // Exit the program with an error code;
  }

  // Fill the data
  strcpy(cars[0].brand, "Ford");
  cars[0].year = 2015;

  strcpy(cars[1].brand, "BMW");
  cars[1].year = 2018;

  strcpy(cars[2].brand, "Volvo");
  cars[2].year = 2023;

  // Print the data
  for (int i = 0; i < 3; i++) {
    printf("%s - %d\n", cars[i].brand, cars[i].year);
  }

  free(cars);
  return 0;
}

Try it Yourself »


Growing Arrays Later with realloc()

If you need more elements later, you can resize your dynamic array with realloc(). This may move the block to a new location and returns a new pointer. Always store the result in a temporary pointer first to avoid losing the original memory if reallocation fails.

Example: Expand an array of structs

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct Car {
  char brand[50];
  int year;
};

int main() {
  int count = 2;
  struct Car *cars = (struct Car*) malloc(count * sizeof(struct Car));
  if (cars == NULL) {
    printf("Initial allocation failed.\n");
    return 1;
  }

  // Initialize first 2 cars
  strcpy(cars[0].brand, "Toyota"); cars[0].year = 2010;
  strcpy(cars[1].brand, "Audi");   cars[1].year = 2019;

  // Need one more car -> grow to 3
  int newCount = 3;
  struct Car *tmp = (struct Car*) realloc(cars, newCount * sizeof(struct Car));
  if (tmp == NULL) {
    // 'cars' is still valid here; free it to avoid a leak
    free(cars);
    printf("Reallocation failed.\n");
    return 1;
  }
  cars = tmp;  // use the reallocated block

  // Initialize the new element at index 2
  strcpy(cars[2].brand, "Kia"); 
  cars[2].year = 2022;

  // Print all cars
  for (int i = 0; i < newCount; i++) {
    printf("%s - %d\n", cars[i].brand, cars[i].year);
  }

  free(cars);
  return 0;
}

Try it Yourself »

Note: New space added by realloc() is uninitialized. Be sure to initialize newly added elements (e.g. strcpy(cars[2].brand, "Kia"); cars[2].year = 2022;) before using them.


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.