Introduction to Dynamic Array in C
A dynamic array in C is a type of array that can be resized during program execution. Unlike static arrays, which have a fixed size at the time of declaration, dynamic arrays can increase or decrease their size as needed. This feature of dynamic arrays makes them very useful for applications where the size of the data to be stored is unknown or may change during program execution.
Dynamic arrays in C are implemented using pointers and memory allocation functions such as malloc() and realloc(). When a dynamic array is created, a block of memory is allocated from the heap, and the size of the array is determined by the number of elements it can hold. As elements are added or removed from the array, the size of the array is adjusted by reallocating memory
we can create a dynamic array in c by using the following methods
- Using malloc() Function
- Using calloc() Function
- Resizing Array Using realloc() Function
- Using Variable Length Arrays(VLAs)
- Using Flexible Array Members
Using malloc() Function:
The malloc() Function allocates a block of memory of a specified size. This Function returns a void pointer, which can be typecasted to any other pointer type. Here's an example of using malloc() function.
Create a dynamic array of integers:
#include <stdio.h>
#include <stdlib.h>
int main() {
int size = 5;
int* dynamic_array = (int*) malloc(size * sizeof(int));
// Populate the array
for (int i = 0; i < size; i++) {
dynamic_array[i] = i * 2;
}
// Print the array
for (int i = 0; i < size; i++) {
printf("%d ", dynamic_array[i]);
}
// Free the memory
free(dynamic_array);
return 0;
}
The output of the above code would be:
0 2 4 6 8
Using calloc() Function:
The calloc() Function allocates a block of memory and initializes it to zero. This Function takes two arguments, the number of elements to allocate and the size of each element. Here's an example of using calloc() to create a dynamic array of floats:
#include <stdio.h>
#include <stdlib.h>
int main() {
int size = 5;
float* dynamic_array = (float*) calloc(size, sizeof(float));
// Populate the array
for (int i = 0; i < size; i++) {
dynamic_array[i] = i * 1.5;
}
// Print the array
for (int i = 0; i < size; i++) {
printf("%.2f ", dynamic_array[i]);
}
// Free the memory
free(dynamic_array);
return 0;
}
The output of the above code would be:
0.00 1.50 3.00 4.50 6.00
Resizing Array Using realloc() Function:
The realloc() Function is used to resize a previously allocated block of memory. It takes two arguments, the pointer to the previously allocated memory and the new size of the memory block. Here's an example of using realloc() to resize a dynamic array of characters:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
char* dynamic_array = (char*) malloc(10 * sizeof(char));
strcpy(dynamic_array, "hello");
printf("Before: %s\n", dynamic_array);
// Resize the array
dynamic_array = (char*) realloc(dynamic_array, 20 * sizeof(char));
strcat(dynamic_array, " world");
printf("After: %s\n", dynamic_array);
// Free the memory
free(dynamic_array);
return 0;
}
The output of the code will be :
Before: hello
After: hello, world
Using Variable Length Arrays (VLAs):
VLAs are a C99 feature that allows declaring an array with a variable size. The size of the array is determined at runtime. Here's an example of using a VLA to create a dynamic array of integers:
#include <stdio.h>
int main() {
int size;
printf("Enter the size of the array: ");
scanf("%d", &size);
int dynamic_array[size];
// Populate the array
for (int i = 0; i < size; i++) {
dynamic_array[i] = i * 3;
}
// Print the array
for (int i = 0; i < size; i++) {
printf("%d ", dynamic_array[i]);
}
return 0;
}
The output of the above code would depend on the input value entered by the user. Here's an example:
Enter the size of the array: 4
0 3 6 9
Using Flexible Array Members:
Flexible Array Members (FAMs) is a feature in C99 that allows for creating structures with a variable-length array at the end. This allows creating of a dynamically sized array as part of a larger data structure. Here's an example of using FAMs to create a dynamic array of characters as part of a larger data structure:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
int id;
char name[];
} Person;
int main() {
Person* person = (Person*) malloc(sizeof(Person) + 10 * sizeof(char));
person->id = 1;
strcpy(person->name, "John");
printf("%d: %s\n", person->id, person->name);
// Resize the array
person = (Person*) realloc(person, sizeof(Person) + 20 * sizeof(char));
strcat(person->name, " Doe");
printf("%d: %s\n", person->id, person->name);
// Free the memory
free(person);
return 0;
}
The output of the above code would depend on the input value entered by the user. Here's an example:
1: John
1: John Doe
These are the different methods for creating dynamic arrays in C. Each method has advantages and disadvantages, and the choice of which method to use depends on the program's specific requirements.