From owner-freebsd-current@FreeBSD.ORG Fri Jul 11 12:08:50 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 D6BB137B401 for ; Fri, 11 Jul 2003 12:08:50 -0700 (PDT) Received: from bremen.shuttle.de (bremen.shuttle.de [194.95.249.251]) by mx1.FreeBSD.org (Postfix) with ESMTP id 7214A43F85 for ; Fri, 11 Jul 2003 12:08:49 -0700 (PDT) (envelope-from schweikh@schweikhardt.net) Received: from bremen.shuttle.de (localhost [127.0.0.1]) by bremen.shuttle.de (Postfix) with ESMTP id 2118A17D20; Fri, 11 Jul 2003 21:08:47 +0200 (CEST) Received: (from uucp@localhost)h6BJ8kKQ023492; Fri, 11 Jul 2003 21:08:46 +0200 Received: (from schweikh@localhost) by hal9000.schweikhardt.net (8.12.9/8.12.9) id h6BJ57WI069706; Fri, 11 Jul 2003 21:05:07 +0200 (CEST) (envelope-from schweikh) Date: Fri, 11 Jul 2003 21:05:07 +0200 From: Jens Schweikhardt To: Terry Lambert Message-ID: <20030711190507.GA69284@schweikhardt.net> References: <20030710011956.A61125@xorpc.icir.org> <3F0D42FC.81DECF87@mindspring.com> Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <3F0D42FC.81DECF87@mindspring.com> User-Agent: Mutt/1.4.1i cc: Luigi Rizzo 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: Fri, 11 Jul 2003 19:08:51 -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; # > # > 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); Huh? next has type pointer-to-void so a cast is never necessary to assign to it. This is the raison d'être for void pointers in C. In standardese void is an incomplete type that can not be completed. Incomplete types have no size. Therefore, any arithmetic on pointer-to-void is undefined. One needs to cast to a non-void pointer before doing arithmetic. Whether this is a char* or other depends on what you want to do. Some compilers by default assume sizeof(void) = 1 (e.g. the same as char in all its qualifications) for simplicity, but this is far from portable. Regards, Jens -- Jens Schweikhardt http://www.schweikhardt.net/ SIGSIG -- signature too long (core dumped)