sizeof a struc field

Lloyd Kremer kremer at cs.odu.edu
Tue Oct 17 06:04:11 AEST 1989


In article <5752 at merlin.usc.edu> jeenglis at nunki.usc.edu (Joe English) writes:

>davidsen at crdos1.UUCP (bill davidsen) writes:
>>  Better yet, can someone post a method to get the size of a field
>>without having to create the struct or union which *is*
>>	a. portable
>>	b. readable
>>
>>  Why discuss the virtues of NULL and why it should be special in this
>>case, assume that even if it works people will flame you for using it,
>>and let someone prove how smart they are by posting a solution to the
>>problem.
>
>
>struct foo {
>	...
>	sometype field;
>	...
>};
>
>Use 'sizeof(sometype)' instead of 'sizeof(((struct foo *)0)->field)'


Neither is adequate if you wish to know the effective size of the member
including structure padding.

On my 32-bit machine running System V UNIX I get:

main()
{
	struct foo {
		char field;
	};

	printf("sizeof(char) == %d\n", sizeof(char));
	printf("sizeof(field) == %d\n", sizeof(((struct foo *)0)->field));
	printf("sizeof(struct foo) == %d\n", sizeof(struct foo));
	return(0);
}

Output:
sizeof(char) == 1
sizeof(field) == 1
sizeof(struct foo) == 4

-- 
					Lloyd Kremer
					...!uunet!xanth!kremer
					Have terminal...will hack!



More information about the Comp.lang.c mailing list