Path: csiph.com!news.swapon.de!eternal-september.org!feeder.eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Tim Rentsch Newsgroups: comp.lang.c Subject: Re: Offsetof with array elements Date: Sun, 16 Feb 2020 02:41:58 -0800 Organization: A noiseless patient Spider Lines: 20 Message-ID: <86wo8mg6fd.fsf@linuxsc.com> References: <87mu9ogkcq.fsf@bsb.me.uk> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: reader02.eternal-september.org; posting-host="5afe6ca82d77f883ab9def33834e0994"; logging-data="11607"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18hVCDmaJZ4AjqB1+HXmRcm9oLuHN6AxH8=" User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) Cancel-Lock: sha1:m0GCmCikQf60pZN6JPqttCPwmIg= sha1:GMKtFu53gwF4nZItC4TkyJVgGUs= Xref: csiph.com comp.lang.c:150041 Ben Bacarisse writes: > Bonita Montero writes: > >> How about this: >> >> #include >> #define array_offsetof(type, member, index) (size_t)(offsetof(type, >> member) + sizeof(((type *)NULL)->##member[0]) * index) > > The ## is wrong. Maybe it was typo? There are no valid tokens that > start -> (other than -> itself) so pasting any other token to -> will > always be an error. > > It has undefined behaviour (dereferencing a null pointer) but may well > work. [...] Why do you say the pointer dereference has undefined behavior? The -> operator is part of an operand to 'sizeof', and hence is not evaluated.