why cant I print out the numbers by writing *p[3]
instead of just p[3]
The expression p[3]
is, by definition of the subscript operator []
, equivalent to *(p+3)
, which means that the int
element that exists 3 elements after the element pointed to by p
is retrieved.
Therefore, *p[3]
is equivalent to **(p+3)
, which does not make sense, because *(p+3)
is an object of type int
, which cannot be dereferenced. Only pointers can be dereferenced.
And why isn't it int *p = &array
instead of int *p = array
?
In the declaration int *p = array;
, the expression array
will decay to a pointer to the first element of the array, i.e. to &array[0]
. Therefore, p
will point to the first element of the array.
However, if you write int *p = &array;
instead, then array
will not decay to &array[0]
. Instead, the expression &array
will evaluate to a pointer to the entire array, not to a pointer to the first element of the array. This means that the type of the pointer is not int *
(pointer to a single int
), but rather int (*)[5]
(pointer to an array of 5 int
elements). A pointer of type int (*)[5]
cannot be assigned to a pointer of type int *
without an explicit cast. For this reason, the line int *p = &array;
is not valid.