Answer:
When you try to print values from the array before setting free the memory previously allocated, it will return values that were inserted while runtime.
For this it is used the calloc, that is a function of the stdlib.h library, of the C programming language. Its objective is to create a vector of dynamic size, that is, defined during the execution of the program. It differs from the malloc function, also from C, because in addition to initializing the memory spaces, it also assigns the value 0 (zero) to each one. It is useful, because in C when a variable is declared, the space in the memory map used by it probably contains some garbage value.
Code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int* ptr;
int n, i;
n = 15;
ptr = (int*)calloc(n, sizeof(int));
if (ptr == NULL) {
printf("\\ Memory is not allocated");
exit(0);
}
else {
printf("\\ Memory allocated");
for (i = 0; i < n; i++) {
printf("\\ Input value in the array at position %d: ",i);
scanf("%d",&ptr[i]);
}
printf("\\ The elements of the array are: ");
for (i = 0; i < n; i++) {
printf("%d, ", ptr[i]);
}
free(ptr);
}
return 0;
}