Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

sizeof a structure member

62 views
Skip to first unread message

sig...@my-deja.com

unread,
Oct 26, 2000, 4:43:01 PM10/26/00
to
Is there a way to directly determine the sizeof a member of a
structure (without using an additional typedef, et cetera)?

For example

struct blah {
int a;
char b[20];
}

I want the sizeof "blah.b", without having to declare a "blah",
nor having to create a separate type for "b". Is there a
way to do this? Intuitively this might be

sizeof(blah.b)

but that doesn't work.


Sent via Deja.com http://www.deja.com/
Before you buy.

Kaz Kylheku

unread,
Oct 26, 2000, 5:02:21 PM10/26/00
to
On Thu, 26 Oct 2000 20:43:01 GMT, sig...@my-deja.com <sig...@my-deja.com> wrote:
>Is there a way to directly determine the sizeof a member of a
>structure (without using an additional typedef, et cetera)?
>
>For example
>
> struct blah {
> int a;
> char b[20];
> }
>
>I want the sizeof "blah.b", without having to declare a "blah",
>nor having to create a separate type for "b". Is there a
>way to do this?

sizeof ((struct blah *) 0)->b;

The expression under sizeof is not evaluated, so no null pointer dereference
actually takes place here.

--
Any hyperlinks appearing in this article were inserted by the unscrupulous
operators of a Usenet-to-web gateway, without obtaining the proper permission
of the author, who does not endorse any of the linked-to products or services.

Morris Dovey

unread,
Oct 26, 2000, 4:14:16 PM10/26/00
to
sig...@my-deja.com wrote:

> Is there a way to directly determine the sizeof a member of a
> structure (without using an additional typedef, et cetera)?
>
> For example
>
> struct blah {
> int a;
> char b[20];
> }
>
> I want the sizeof "blah.b", without having to declare a "blah",
> nor having to create a separate type for "b". Is there a
> way to do this? Intuitively this might be
>
> sizeof(blah.b)
>
> but that doesn't work.

Try just using the constant 20 -- or you could #define BLAH_B_SIZE 20
and replace 20 where you need it with BLAH_B_SIZE -- or you could take
advantage of the typedef option (why not?).

Morris Dovey
West Des Moines, Iowa USA

Morris Dovey

unread,
Oct 26, 2000, 4:17:39 PM10/26/00
to
Kaz Kylheku wrote:

> sizeof ((struct blah *) 0)->b;
>
> The expression under sizeof is not evaluated, so no null pointer dereference
> actually takes place here.

Kaz...

Most clever. (I guess that's what comes of reading the language specification.)

-hs-

unread,
Oct 26, 2000, 5:36:30 PM10/26/00
to
Morris Dovey a écrit dans le message <39F89162...@iedu.com>...

>Kaz Kylheku wrote:
>
>> sizeof ((struct blah *) 0)->b;
>>
>> The expression under sizeof is not evaluated, so no null pointer
dereference
>> actually takes place here.
>
>Kaz...
>
>Most clever. (I guess that's what comes of reading the language
specification.)


Yes, it's fine.

Have a look to the ISO-C offsetof() macro in <stddef.h>
http://www.dinkum.com/htm_cl/stddef.html#offsetof
It is based on the same idea.

--
-hs- Tabs out, spaces in.
CLC-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
ISO-C Library: http://www.dinkum.com/htm_cl
FAQ de FCLC : http://www.isty-info.uvsq.fr/~rumeau/fclc

Kaz Kylheku

unread,
Oct 26, 2000, 5:37:59 PM10/26/00
to
On Thu, 26 Oct 2000 23:36:30 +0200, -hs- <email....@server.invalid> wrote:
>Morris Dovey a écrit dans le message <39F89162...@iedu.com>...
>>Kaz Kylheku wrote:
>>
>>> sizeof ((struct blah *) 0)->b;
>>>
>>> The expression under sizeof is not evaluated, so no null pointer
>dereference
>>> actually takes place here.
>>
>>Kaz...
>>
>>Most clever. (I guess that's what comes of reading the language
>specification.)
>
>
>Yes, it's fine.
>
>Have a look to the ISO-C offsetof() macro in <stddef.h>
>http://www.dinkum.com/htm_cl/stddef.html#offsetof

There is no ISO C macro in <stddef.h>. ISO C macros live in the ISO C document.

:)

The null pointer hack used by implementations of offsetof() is not
well-defined; it relies on pointer arithmetic involving the null pointer.

The null pointer hack in the trick I used is defined, because the pointer is
never evaluated.

Morris Dovey

unread,
Oct 26, 2000, 5:22:33 PM10/26/00
to
Kaz Kylheku wrote:

> The null pointer hack used by implementations of offsetof() is not
> well-defined; it relies on pointer arithmetic involving the null pointer.
>
> The null pointer hack in the trick I used is defined, because the pointer is
> never evaluated.

Kaz...

I don't care if it /is/ a "hack". Don't even care that /you/ produced it. It's
beautiful! (I stole it.)

sig...@my-deja.com

unread,
Oct 27, 2000, 12:18:20 PM10/27/00
to
In article <slrn8vh78...@ashi.FootPrints.net>,
k...@ashi.footprints.net wrote:

> On Thu, 26 Oct 2000 20:43:01 GMT, sig...@my-deja.com <sig225@my-
deja.com> wrote:

> >I want the sizeof "blah.b", without having to declare a "blah",
> >nor having to create a separate type for "b". Is there a
> >way to do this?
>
> sizeof ((struct blah *) 0)->b;

That is what I wanted, thanks.

0 new messages