#include <stdio.h>
#include <stdlib.h>
int main() {
int *arr; // Step 1: Declare a pointer variable to store the base address of the dynamically allocated memory block.
int size; // Step 2: Declare a variable to store the size of the array.
printf("Enter the size of the array: ");
scanf("%d", &size); // Step 3: Read the size of the array from the user.
arr = (int )malloc(size sizeof(int)); // Step 4: Allocate memory dynamically using malloc.
if (arr == NULL) {
printf("Memory allocation failed. Exiting...");
return 1; // Step 5: Check if memory allocation is successful.
}
printf("Memory allocated successfully. Enter %d elements:\n", size);
for (int i = 0; i < size; ++i) {
scanf("%d", &arr[i]); // Step 6: Read elements of the array from the user.
}
printf("Elements stored in the dynamically allocated array are:\n");
for (int i = 0; i < size; ++i) {
printf("%d ", arr[i]); // Step 7: Display the elements of the dynamically allocated array.
}
free(arr); // Step 8: Deallocate the dynamically allocated memory.
return 0;
}