Why a statement like *B[0] doesn't give the address of the first 1D Array?
Because the indirection operator (*
) doesn't return the address of the operand. It does the opposite: It returns the pointed value.
So, since the precedence of subscript operator is higher, B[0]
is done first. It results in the first subarray. In the expression *B[0]
, that subarray decays to pointer to first element of the subarray, and you indirect that pointer to get the pointed value. You failed to initialise that value, so the behaviour of accessing it is undefined.
In the case of a pointer or an array, *arr
is equivalent to arr[0]
. Therefore *B[0]
is equivalent to B[0][0]
.
PS.
%d
is not the correct format specifier for pointers. You need to use %p
to avoid undefined behaviour. That will use hexadecimal base. If you insist on decimal, you can convert the pointer to std::uintptr_t
, and use the PRIuPTR
macro for the format specifier.
But the name of the array is a pointer to the first element of the array.
No, it is not. However, in many cases the array will be implicitly converted to the pointer to first element. This conversion is called decaying.