public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* To get the size of a member of a struct
@ 2008-04-17 19:19 Eus
  2008-04-18 19:52 ` Jim Wilson
  0 siblings, 1 reply; 3+ messages in thread
From: Eus @ 2008-04-17 19:19 UTC (permalink / raw)
  To: GCC Help Mailing List

Hi Ho!

Is `sizeof(((struct person *) 0)->name)' the proper way to retrieve the size of a
member of a struct without declaring a variable of that struct as demonstrated in
the program below? It is because I cannot do `sizeof(person->name)' or
`sizeof((struct person).name)'. Or, is there any other way to do so?

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

int main (int argc, char **argv, char **envp)
{
        printf ("sizeof(name) = %zd\n", sizeof(((struct person *) 0)->name));

        exit (EXIT_SUCCESS);
}

Thank you very much.

Best regards,
Eus


      ____________________________________________________________________________________
Be a better friend, newshound, and 
know-it-all with Yahoo! Mobile.  Try it now.  http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: To get the size of a member of a struct
  2008-04-17 19:19 To get the size of a member of a struct Eus
@ 2008-04-18 19:52 ` Jim Wilson
  2008-04-19 10:30   ` Eus
  0 siblings, 1 reply; 3+ messages in thread
From: Jim Wilson @ 2008-04-18 19:52 UTC (permalink / raw)
  To: eus; +Cc: GCC Help Mailing List

Eus wrote:
> Is `sizeof(((struct person *) 0)->name)' the proper way to retrieve the size of a
> member of a struct without declaring a variable of that struct as demonstrated in
> the program below?

It should work, but may not be the best solution.

Another solution is to define a type you can query, e.g. 

typedef char name_t[50];
and then use this in the struct definition and in the sizeof call.

Jim

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: To get the size of a member of a struct
  2008-04-18 19:52 ` Jim Wilson
@ 2008-04-19 10:30   ` Eus
  0 siblings, 0 replies; 3+ messages in thread
From: Eus @ 2008-04-19 10:30 UTC (permalink / raw)
  To: Jim Wilson; +Cc: GCC Help Mailing List

Hi Ho!

--- Jim Wilson <wilson@tuliptree.org> wrote:
> 
> It should work, but may not be the best solution.
> 
> Another solution is to define a type you can query, e.g. 
> 
> typedef char name_t[50];
> and then use this in the struct definition and in the sizeof call.

I think this is good. But, not everyone likes to use typedef, for example, Linus
Torvalds as he mentioned not to use typedef in the Linux kernel coding guideline
:)

> Jim

Best regards,
Eus


      ____________________________________________________________________________________
Be a better friend, newshound, and 
know-it-all with Yahoo! Mobile.  Try it now.  http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2008-04-19  7:03 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-04-17 19:19 To get the size of a member of a struct Eus
2008-04-18 19:52 ` Jim Wilson
2008-04-19 10:30   ` Eus

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).