VOOZH about

URL: https://www.geeksforgeeks.org/c/initialize-a-dynamic-array-in-c/

⇱ How to Initialize a Dynamic Array in C? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Initialize a Dynamic Array in C?

Last Updated : 23 Jul, 2025

In C, dynamic memory allocation is done to allocate memory during runtime. This is particularly useful when the size of an array is not known at compile time and needs to be specified during runtime. In this article, we will learn how to initialize a dynamic array in C.

Initializing a Dynamic Arrays in C

in C, can be initialized at the time of their declaration by using the function that allocates a block of memory and returns a pointer to it, or function that allocates memory for the given size and initializes all bytes to zero. After allocation, we can initialize the elements of the array using the .

C Program to Dynamically Initialize an Array

The below example demonstrates the use of the malloc function to initialize a dynamic array in C.


Output

Enter the size of the array: 5
Elements of the array are: 1 2 3 4 5

Time Complexity: O(n), where n is the size of the dynamically allocated array.
Auxiliary Space: O(n)

Note: Always remember to deallocate the memory using the function after use to prevent memory leaks.

Comment