Groovy hepcat Jack was jivin' on 19 Jul 2006 08:51:05 -0700 in
comp.lang.c.
sizeof()'s a cool scene! Dig it!
For the structure below:
struct A{
int a;
double b;
char c;
};
struct A sa;
printf("sizeof(A): %d\n", sizeof(sa));
The output is
sizeof(A): 16
Why it is 16?
Who can tell? Since you are invoking undefined behaviour, anything
could happen. You could get 99.99999 as output, though that's highly
unlikely. You could get pink snow falling in your bedroom, though
that's even less likely. Much more likely is a bogus integer output
such as 0, or a crash, or the number of bytes in the structure (which
may be 16) being output. You can never be sure with undefined
behaviour.
Remember, sizeof returns a value of type size_t, an implementation
defined unsigned integer type. It might be unsigned int, unsigned long
or even unsigned char or some other type. C99 defines a printf()
conversion specifier for size_t. If you have a C99 implementation and
don't care about backward compatibility with C90, you can use that.
Otherwise, you should cast the result of sizeof to a known type
(preferebly unsigned long) and use the coresponding conversion
specifier.
printf("sizeof sa: %lu\n", (unsigned long)sizeof sa);
Then your output will be meaningful.
--
Dig the even newer still, yet more improved, sig!
http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?