How to Initialize a Dynamic Array in C? Last Updated : 27 Jun, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 CDynamic Arrays in C, can be initialized at the time of their declaration by using the malloc() function that allocates a block of memory and returns a pointer to it, or calloc() 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 for loop.C Program to Dynamically Initialize an ArrayThe below example demonstrates the use of the malloc function to initialize a dynamic array in C. C // C program to demonstrate how to dynamically allocate // and initialize an array #include <stdio.h> #include <stdlib.h> int main() { // Take array size as input from user printf("Enter the size of the array: "); int size; scanf("%d", &size); // Dynamically allocate an array int* arr = (int*)malloc(size * sizeof(int)); // Check if the memory has been successfully allocated if (arr == NULL) { printf("Memory not allocated.\n"); return 1; // Exit the program } // Initialize values to the array elements for (int i = 0; i < size; i++) { arr[i] = i + 1; } // Print the array elements printf("Elements of the array are: "); for (int i = 0; i < size; i++) { printf("%d ", arr[i]); } printf("\n"); // Deallocate the memory free(arr); return 0; } OutputEnter the size of the array: 5Elements 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 free() function after use to prevent memory leaks. Comment More infoAdvertise with us Next Article How to Initialize a 2D Array in C? R rohitpant4532 Follow Improve Article Tags : C Programs C Language c-array C-Arrays C-Dynamic Memory Allocation C Examples +2 More Similar Reads How to Initialize a 2D Array in C? In C, a 2D Array is a type of multidimensional array in which data is stored in tabular form (rows and columns). It has two dimensions so it can store the data and can expand in two directions. In this article, we will learn how to initialize a 2D array in C.There are three main ways to initialize t 4 min read How to Initialize a 3D Array in C? In C, a 3D array is a type of multidimensional array that stores data in a three-dimensional grid. It has three dimensions, allowing it to store data in three directions: rows, columns, and depth. In this article, we will learn how to initialize a 3D array in C.There are three ways in which a 3D arr 4 min read How to Initialize Array to 0 in C? Initializing an array to zero is a common practice in programming to ensure that all elements start with a known value. In C, there are several ways to initialize an array to zero. In this article, we will explore different methods to do so. Initialize Array to 0 in CThere are mainly two ways to ini 2 min read How to Find Size of Dynamic Array in C? In C, dynamic memory allocation allows us to manage memory resources during the execution of a program. Itâs particularly useful when dealing with arrays where the size isnât known at compile time. In this article, we will learn how to find the size of a dynamically allocated array in C. Find Size o 2 min read How to Initialize Char Array in Struct in C? In C++, we can also define a character array as a member of a structure for storing strings. In this article, we will discuss how to initialize a char array in a struct in C. Initialization of Char Array in Struct in CWhen we create a structure instance (variable) in C that includes a char array, we 2 min read How to Initialize Array of Structs in C? In C, arrays are data structures that store the data in contiguous memory locations. While structs are used to create user-defined data types. In this article, we will learn how to initialize an array of structs in C. Initializing Array of Structures in CWe can initialize the array of structures usi 2 min read Like