41 lines
895 B
C
41 lines
895 B
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
int main() {
|
|
int *ptr;
|
|
int n, i;
|
|
|
|
printf("Enter the number of elements: ");
|
|
scanf("%d", &n);
|
|
// Allocate memory for n integers
|
|
ptr = (int *)malloc(n * sizeof(int));
|
|
|
|
// Check if memory has been successfully allocated
|
|
if (ptr == NULL) {
|
|
printf("Memory not allocated.\n");
|
|
return 1; // Exit the program
|
|
}
|
|
|
|
// Memory has been successfully allocated
|
|
printf("Memory successfully allocated using malloc.\n");
|
|
|
|
// Get the elements
|
|
for (i = 0; i < n; i++) {
|
|
printf("Enter element %d: ", i + 1);
|
|
scanf("%d", &ptr[i]);
|
|
}
|
|
|
|
// Print the elements
|
|
printf("The elements are: ");
|
|
for (i = 0; i < n; i++) {
|
|
printf("%d ", ptr[i]);
|
|
}
|
|
printf("\n");
|
|
|
|
// Free the allocated memory
|
|
free(ptr);
|
|
printf("Memory successfully freed.\n");
|
|
|
|
return 0;
|
|
}
|