#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 scanf if this array is not needed? p
is a pointer to all memory spaces, therefore &p[i]
should indicate the address of the pointer, but not where we want to store the data correctly? Also, if we write *p[i]
in printf
, it gives an error, p
is a pointer, so we have to relate to it and store the data in the reserved space in memory, but it does not work? Even if I compile the above program, since it stops working after entering 3 values as input.
source share