Date: Thu, 10 Jul 2003 03:42:04 -0700 From: Terry Lambert <tlambert2@mindspring.com> To: Luigi Rizzo <rizzo@icir.org> Cc: current@freebsd.org Subject: Re: what is the suggested way to do void * arithmetic ? Message-ID: <3F0D42FC.81DECF87@mindspring.com> References: <20030710011956.A61125@xorpc.icir.org>
next in thread | previous in thread | raw e-mail | index | archive | help
Luigi Rizzo wrote: > in several places in ipfw2.c i have to move pointers across > structures of variable length (lists of ipfw2 instructions > returned by the getsockopt()), and i use the following type of code: > > void *next; > foo *p; > next = (void *)p + len; > foo = (foo *)p + len; > > When using WARNS=5, the compiler in -current flags them with 'Warning > void * arithmetic'. > > What is the best way to do the above given that i do need to use > these variable-size structures ? I don't understand the second one. The first one blows up because you aren't parenthesizing, e.g.: next = (void *)(p + len); The compiler is complaining because it doesn't know sizeof(*((void *)0)) (pointer arithmatic is coerced to the type of the lvalue, in most cases of casts). Unless you are referencing them as array elements (in which case, packing becomes a problem for you, when referencing them as arrays of foo's, since you don't know how foo's are packed in an array), you should probably cast them to char for the arithmatic, and add them with byte counts. -- Terry
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?3F0D42FC.81DECF87>