Path: utzoo!censor!geac!torsqnt!hybrid!scifi!bywater!uunet!stan!imp From: imp@Solbourne.COM (Warner Losh) Newsgroups: comp.lang.c Subject: Re: Turbo C++ 1.0 Huge pointer addition error? Message-ID: <1991Jan14.012118.5186@Solbourne.COM> Date: 14 Jan 91 01:21:18 GMT References: <9101122049.AA28040@lilac.berkeley.edu> Organization: Solbourne Computer, Inc. Lines: 28 In article <9101122049.AA28040@lilac.berkeley.edu> C512052@UMCVMB.BITNET ("David K. Drum") writes: >typedef struct { . . . } foo; [+]>foo huge *hp1,hp2; >hp1 = (foo huge *) calloc(sizeof(foo),16); /* 16 foo structures */ >/* Do something with foo structure # 1 */ [*]>hp2 = hp1 + sizeof(foo); >. . . [+] should be foo huge *hp1, *hp2 [*] should be hp2 = hp1 + 1 The rules of pointer arithmetic in 'C' state that when you are adding a number to the poitner, you are not really adding that number, but instead you are asking the compiler for a pointer to the nth object past the pointer. Turbo 'C' is doing exactly what you told it to do in squaring the sizeof (foo). It is giving you the sizeof(foo)th object past the current object of hp1. It is not a bug, so I don't think you can get a fix for it from Borland :-) Read K&R's section on pointer math, or re-read the section on pointer math in your Turbo C++ manuals before proceding. Warner Losh The Imp imp@Solbourne.COM -- Warner Losh imp@Solbourne.COM We sing about Beauty and we sing about Truth at $10,000 a show.