![]() |
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.
Enter the size of the array: 5 Time Complexity: O(n), where n is the size of the dynamically allocated array.
|
Reffered: https://www.geeksforgeeks.org
C Language |
Related |
---|
![]() |
![]() |
![]() |
![]() |
![]() |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 19 |