I'm trying to write a program that allows the user to enter in the size of the array, then the values of the elements and then print them out. After running it the program prints out the memory locations instead of the values and then errors out.
Code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int size;
int i;
int *ptr;
// int val;
printf("Enter number of elements: ");
scanf("%d",&size);
ptr= malloc(size*sizeof(*ptr)); //memory allocated using malloc
if(ptr==NULL)
{
printf("Error! memory not allocated.");
exit(0);
}
printf("Enter elements of array: \n");
for(i=0;i<size;++i)
{
scanf("%d",ptr[i]);
}
for(i=0;i<size;i++)
{
printf("%d : %d.\n", i+1, ptr[i]);
}
free(ptr);
return 0;
}