From owner-freebsd-current@FreeBSD.ORG Thu Jul 10 09:40:59 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 2075237B401 for ; Thu, 10 Jul 2003 09:40:57 -0700 (PDT) Received: from xorpc.icir.org (xorpc.icir.org [192.150.187.68]) by mx1.FreeBSD.org (Postfix) with ESMTP id 3DA2043FAF for ; Thu, 10 Jul 2003 09:40:57 -0700 (PDT) (envelope-from rizzo@xorpc.icir.org) Received: from xorpc.icir.org (localhost [127.0.0.1]) by xorpc.icir.org (8.12.8p1/8.12.3) with ESMTP id h6AGevkN037775; Thu, 10 Jul 2003 09:40:57 -0700 (PDT) (envelope-from rizzo@xorpc.icir.org) Received: (from rizzo@localhost) by xorpc.icir.org (8.12.8p1/8.12.3/Submit) id h6AGeu2m037774; Thu, 10 Jul 2003 09:40:56 -0700 (PDT) (envelope-from rizzo) Date: Thu, 10 Jul 2003 09:40:56 -0700 From: Luigi Rizzo To: Terry Lambert Message-ID: <20030710094056.A73538@xorpc.icir.org> References: <20030710011956.A61125@xorpc.icir.org> <3F0D42FC.81DECF87@mindspring.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.2.5.1i In-Reply-To: <3F0D42FC.81DECF87@mindspring.com>; from tlambert2@mindspring.com on Thu, Jul 10, 2003 at 03:42:04AM -0700 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 16:40:59 -0000 On Thu, Jul 10, 2003 at 03:42:04AM -0700, Terry Lambert wrote: > 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; ^^^^^^^^^^^^^^ sorry i meant p = (void *)p + len; ... > 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)) ok, it actually evaluates to 1 and i thought it was some standard, probably it is not so i guess i have to cast to (char *) instead thanks luigi > (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