In <11*************************@posting.google.com>
sw************@yahoo.co.in (Sweety) writes:
Except for the trivial
#define SIZEOF sizeof
sizeof(type) cannot be replaced by a macro (portably). It would
have to use a trick similar to that used by the canonical offsetof
implementation (pointer arithmetic on a null pointer of the type
in question):
#define TSIZEOF(x) ((char *)((x *)0 + 1) - (char *)(x *)0)
What can be done portably is replacing sizeof lvalue by a macro:
#define LVSIZEOF(x) ((char *)(&(x) + 1) - (char *)&(x))
Neither of these macros has the right type for sizeof, but this can be
trivially fixed.
The part that can't be implemented in standard C as a macro at all is
sizeof rvalue. This can be reduced to TSIZEOF with a GNU C extension:
#define RVSIZEOF(x) TSIZEOF(typeof(x))
Remeber: only LVSIZEOF is written in portable C, although TSIZEOF is
likely to work practically anywhere.
Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email:
Da*****@ifh.de