Path: utzoo!censor!geac!torsqnt!news-server.csri.toronto.edu!cs.utexas.edu!asuvax!noao!ncar!midway!uwvax!sevenlayer.cs.wisc.edu!bothner From: bothner@sevenlayer.cs.wisc.edu (Per Bothner) Newsgroups: comp.lang.c++ Subject: Re: Pointer arithmetic Keywords: char* vs void* pointer arithmitic Message-ID: <1991Jan4.211448.27626@spool.cs.wisc.edu> Date: 4 Jan 91 21:14:48 GMT References: <1991Jan4.191051.3995@demott.com> Sender: news@spool.cs.wisc.edu (The News) Organization: U of Wisconsin CS Dept Lines: 22 In article <1991Jan4.191051.3995@demott.com> kdq@demott.COM (Kevin D. Quitt) writes: > > What about the subtraction of pointers to get a constant? I discovered >that gcc does not allow: > int = strchr( string, char ) - &string; >to determine the position of char within string. Is there a (good) reason >for disallowing this? Perhaps because it's wrong? It should be: int i = strchr( str, ch ) - str; (Note: no '&' before the string.) Also, you need a prototype for strchr, so that the compiler can know that the return type is (char*) and not (int). libg++ has such a prototype in . Once you make these fixes, I'm sure gcc/g++ will allow your example. -- --Per Bothner bothner@cs.wisc.edu Computer Sciences Dept, U. of Wisconsin-Madison