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.
It will always return 1.
An array is a contiguously allocated non-empty set of objects with the element type – arrays are not allowed to have padding before or between elements. It follows that a pointer to an array points at the same location as a pointer to the first element, although it has a different type.
&buffis the address of the variablebuff. The variablebuffin this case is an array, so&buffis the address of an array – and just like the address of astructvariable is the same location as the address of its first member (although a different type), the address of an array variable is the same location as the address of its first member.§6.5.9 tells us:
In this case the “pointer to an object” is the pointer to the array, and the “pointer to the subobject at its beginning” is the pointer to the first element of the array.
In other words, it returns 1 for much the same reason that this code does: