I understand how an array would decay into a pointer to the first element under certain circumstance.
I know that int array[] = {1,2,3,4,5}
creates an array and that we can create a pointer int *ptr = array
that after the assignment, also points to the first element of the array.
The thing is, what would happen when I do:int *ptr = {1,2,3,4,5}
?
I tried printf("%d",ptr)
,printf("%d",ptr+1)
and printf("%d",ptr+2)
and it returns 1
,5
and 9
respectively.
I tried printf("%d",*ptr)
and it returns a segmentation fault
So I see that ptr
points to the array {1,2,3,4,5}
, and again tried:
for(int i=0;i<5;i++){
printf("%d",(*p)[i]);
}
and it returns subscripted value is neither array nor pointer nor vector
Then I googled and can't really find what I am looking for.