login
Header Space

 
 

Getting the size of a member of a struct or union

May 18, 2008 - 11:07am
Submitted by Eus on May 18, 2008 - 11:07am.

Looking at Getting the offset of a member in a struct, I realized that you can also use the same technique to get the size of a member of a struct or a union without declaring a variable of that struct. Suppose a struct has been defined as follows:

struct person
{
	int id;
	char name [50];
	double salary;
};

You cannot do sizeof(person->name) or sizeof((struct person).name). But, you can do sizeof(((struct person *) 0)->name).

Archive: All about C Programming

speck-geostationary