We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
In C, dynamic arrays are implemented using pointers and memory allocation functions like malloc, realloc, and free. Unlike fixed-size arrays, dynamic arrays can grow or shrink in size during runtime, making them more flexible when you don't know the exact size of the array in advance.
Here's a basic example of how to create and use a dynamic array in C:
c
Copy code
include
include
int main() {
int *dynamicArray; // Declare a pointer to int (dynamic array)
int size = 5; // Initial size of the array
// Allocate memory for the dynamic array using malloc
dynamicArray = (int *)malloc(size * sizeof(int));
if (dynamicArray == NULL) {
printf("Memory allocation failed. Exiting...\n");
return 1;
}
// Access elements of the dynamic array as if it were a regular array
for (int i = 0; i < size; i++) {
dynamicArray[i] = i * 10;
}
// Print the elements of the dynamic array
printf("Dynamic Array Elements: ");
for (int i = 0; i < size; i++) {
printf("%d ", dynamicArray[i]);
}
printf("\n");
// Modify the size of the dynamic array using realloc
size = 10; // New size of the array
dynamicArray = (int *)realloc(dynamicArray, size * sizeof(int));
if (dynamicArray == NULL) {
printf("Memory reallocation failed. Exiting...\n");
return 1;
}
// Access and print the modified elements
for (int i = 5; i < size; i++) {
dynamicArray[i] = i * 10;
}
printf("Modified Dynamic Array Elements: ");
for (int i = 0; i < size; i++) {
printf("%d ", dynamicArray[i]);
}
printf("\n");
// Don't forget to free the allocated memory when done using the array
free(dynamicArray);
return 0;
}
This code declares a dynamic array of integers using malloc with an initial size of 5. It then accesses and prints the elements of the array before using realloc to resize the array to a new size of 10. After that, it accesses and prints the modified elements of the array. Finally, it frees the allocated memory using free to prevent memory leaks.
Remember that when using dynamic arrays, it's essential to manage
memory properly to avoid memory leaks or undefined behavior. Always free the memory using free when you're done using the array. Also, remember to check if memory allocation (malloc and realloc) was successful to handle potential failures gracefully.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Dynamic Array in C
You are viewing a single comment's thread. Return to all comments →
In C, dynamic arrays are implemented using pointers and memory allocation functions like malloc, realloc, and free. Unlike fixed-size arrays, dynamic arrays can grow or shrink in size during runtime, making them more flexible when you don't know the exact size of the array in advance.
Here's a basic example of how to create and use a dynamic array in C:
c Copy code
include
include
int main() { int *dynamicArray; // Declare a pointer to int (dynamic array) int size = 5; // Initial size of the array
} This code declares a dynamic array of integers using malloc with an initial size of 5. It then accesses and prints the elements of the array before using realloc to resize the array to a new size of 10. After that, it accesses and prints the modified elements of the array. Finally, it frees the allocated memory using free to prevent memory leaks.
Remember that when using dynamic arrays, it's essential to manage memory properly to avoid memory leaks or undefined behavior. Always free the memory using free when you're done using the array. Also, remember to check if memory allocation (malloc and realloc) was successful to handle potential failures gracefully.