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

前端 未结 5 1404
陌清茗
陌清茗 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:35

    The behavior you see is a result of 'c' interpreting arr as a pointer to the memory location of the first element. Pointer to the array &arr will be an address of the array itself in memory, which is also the address of its first element. As a result arr and &arr yield the same values. Try this:

    printf("array = %p, &array = %p\n", arr, &arr);
    

    Thought the values are the same, types are different. arr is a variable, whether &arr is a pointer to the variable.

    ptr reflects the same picture. Its value ptr = &arr makes it a pointer to an array. It contains the address of the array. *ptr returns the array itself, which in 'c' interpretation is the address of the first element of the array. As a result values for ptr and *ptr are the same as for &arr and arr.

    Hope it makes it clearer (not murkier) :-)

提交回复
热议问题