I was browsing some C++ code recently and I ran into the following line:
static char zsocket_name[sizeof((struct sockaddr_un*)0)->sun_path] = {};
… This is confusing, as it looks to me as if the result of the sizeof
operator is being pointer-dereferenced to access a struct field named sun_path
, and that value is to be used to size an array in static storage.
However, when I tried a simple snippet program to evaulate the expression sizeof((struct sockaddr_un*)0)->sun_path
, it yields the size of the sun_path
member of the sockaddr_un
struct.
Clearly, that is what the author of the original line was intending; but I find it syntactically confusing as it looks like a pointer dereference on the result of the sizeof(…)
operation.
What am I missing about this use of sizeof(…)
? Why does this expression evaluate this way?