0

When I create an array and a pointer to that array, why cant I print out the numbers by writing *p[3] instead of just p[3]. When I am doing it with normal numbers like the variable b in the example, I can only access the pointers value by typing the * operator before e (i.e *e). And why isn't it int *p = &array instead of int *p = array?

#include <iostream>

int main(){

    int array[5] = {3, 3, 4, 6, 7};
    int *p = array;
    std::cout << p << "\n" << p[3];

    int b = 5;
    int *e = &b;
    std::cout << "\n" << e << " " << *e;
}   
Clifford
  • 88,407
  • 13
  • 85
  • 165

2 Answers2

3

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.

Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
  • Removed my answer as your explanation covers every aspect much deeper. Well said. – UpAndAdam Sep 05 '22 at 21:26
  • The classic mind-bender is `3[p]` has the same effect also being equivalent to `*(3+p)` and pointer/integer arithmetic being commutative. – Persixty Sep 05 '22 at 21:49
0

operator[] dereferences the pointer. You can also get a pointed to value by writing e[0]. Using both operator[] and the dereferencing asterisk would dereference twice.

Dan O
  • 4,323
  • 5
  • 29
  • 44