Dynamic memory allocation
Dynamic Memory Allocation Functions
void *malloc(size_t size);#include <stdio.h>
#include <stdlib.h>
int main() {
int *arr = (int *)malloc(5 * sizeof(int)); // Allocate memory for 5 integers
if (arr == NULL) {
printf("Memory allocation failed\n");
return 1;
}
// Use the allocated memory
for (int i = 0; i < 5; i++) {
arr[i] = i * 10;
printf("%d ", arr[i]); // Outputs: 0 10 20 30 40
}
free(arr); // Free the allocated memory
return 0;
}Dynamic Arrays
Summary
Last updated