How does de-referencing work for pointer to an array?

前端 未结 5 1489
陌清茗
陌清茗 2021-01-24 09:53

Pointer to array of elements when dereferenced return an address. Since it is holding the address of the first element of the array, dereferencing it should return a value.

5条回答
  •  不要未来只要你来
    2021-01-24 10:34

    Here is a simple, less technical explanation.

    How are you setting p? = arr;

    How are you setting ptr? = &arr;

    Your question really has nothing to do with arrays at all. arr could be literally any type and the answer would be the same: & gets the address of arr, so whatever arr is, ptr is storing its address while p is storing arr itself.

    If you do *ptr, you will dereference that address and thus get a value equal to p.

    int arr = 3;
    // clearly these are different!
    int p = arr;
    int* ptr = &arr;
    

    Similarly

    int x = 3;
    int* arr = &x;
    // clearly these are different!
    int* p = arr;
    int** ptr = &arr;
    // so of course they dereference differently
    printf("*p = %d, *ptr = %p\n", *p, *ptr);
    

提交回复
热议问题