K
Keith Thompson
Eric Sosman said:IMHO, the correct answer to the O.P.'s question
... is to use `sizeof(X)'. The question doesn't seek to
explore one's knowledge of C, but one's ability to spot
and reject a stupid specification.
I don't *entirely* agree. There are (clumsy) solutions involving
pointer arithmetic. Being able to determine the size of a type
without using sizeof does demonstrate a knowledge of how pointer
arithmetic works in C -- and there could be problems requiring similar
solutions for which the language doesn't provide a built-in operator.
Determining the alignment for a type is one example; offsetof would be
another if it weren't already declared in <stddef.h>.
But of course there is absolutely no reason to determine the size of a
type without using sizeof in the real world. That's what sizeof is
for, after all.
IMHO, a better response to the OP's question is:
*Why* can't you use sizeof?
I wouldn't be inclined to provide a solution until that question is
answered.