#include <stdio.h>
#include <stdlib.h>
int main()
{
int *p, n, i;
printf("Enter the size of the array:");
scanf("%d", &n);
p = (int *)malloc(n * sizeof(int));
for (i = 0; i < n; i++)
{
printf("\n Enter element %d:", i + 1);
scanf("%d", &p[i]);
}
for (i = 0; i < n; i++)
printf("\n %d", p[i]);
return 0;
}
Why do we need to write &
in the scanf, if it's an array it's not necessary?
p
is a pointer to all the memory spaces so &p[i]
should give the address of the pointer but not where we want to store the data right?
Also if we write *p[i]
in printf
, it gives an error, p
is a pointer so we should deference it and store the data in the reserved space in the memory but it's not working? Even if I compile the above program as it is it stops working after taking 3 values as input.