From owner-freebsd-current@FreeBSD.ORG Thu Jul 10 03:43:08 2003 Return-Path: Delivered-To: freebsd-current@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 373F837B401 for ; Thu, 10 Jul 2003 03:43:08 -0700 (PDT) Received: from puffin.mail.pas.earthlink.net (puffin.mail.pas.earthlink.net [207.217.120.139]) by mx1.FreeBSD.org (Postfix) with ESMTP id 9EDF143FBD for ; Thu, 10 Jul 2003 03:43:07 -0700 (PDT) (envelope-from tlambert2@mindspring.com) Received: from user-2ivfjrs.dialup.mindspring.com ([165.247.207.124] helo=mindspring.com) by puffin.mail.pas.earthlink.net with asmtp (SSLv3:RC4-MD5:128) (Exim 3.33 #1) id 19aYtF-0005YA-00; Thu, 10 Jul 2003 03:43:06 -0700 Message-ID: <3F0D42FC.81DECF87@mindspring.com> Date: Thu, 10 Jul 2003 03:42:04 -0700 From: Terry Lambert X-Mailer: Mozilla 4.79 [en] (Win98; U) X-Accept-Language: en MIME-Version: 1.0 To: Luigi Rizzo References: <20030710011956.A61125@xorpc.icir.org> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-ELNK-Trace: b1a02af9316fbb217a47c185c03b154d40683398e744b8a46893e4d8b7973b7152fbecbb1d59d11f2601a10902912494350badd9bab72f9c350badd9bab72f9c cc: current@freebsd.org Subject: Re: what is the suggested way to do void * arithmetic ? X-BeenThere: freebsd-current@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: Discussions about the use of FreeBSD-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 10 Jul 2003 10:43:08 -0000 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