Is the return value of this function well defined by the C standard?
int foo()
{
char buff[128];
// This is the important line:
if ((void*)buff == (void*)(&buff))
return 1;
else
return 0;
}
What is the result of foo? On gcc and clang, it will always be 1, but I do not think that this is guaranteed by the standard.
Are they [
buffand&buff] guaranteed to have the same value or is that something that just happens with gcc and clang? I ask because I don't think &buff is "the address of the start of the array," but the address of the variable buff.
They are guaranteed to be the same value. In C arrays can be considered "special" in the sense that they're not pointers and the only time they're treated as such is when they're passed to a function and they decay to pointers.
For that reason:
buff == &buff && buff == &buff[0]
However buff and &buff are of two different types, where &buff is a pointer to the array (they still refer to the same spot!). You can use sizeof on the two to see this difference for yourself.
That is not the same for a pointer, which as you say will return the address of that actual pointer variable. An array also isn't a pointer to itself since in order for a pointer to point to itself, the address of that pointer would need to be stored in that pointer variable.
An array name is simply a label used by the compiler that corresponds to a contiguous block of memory. It has special properties, the above being one of them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With