Pointer arithmetic

Kevin D. Quitt kdq at demott.com
Sat Jan 5 11:16:07 AEST 1991


    I originally posted to comp.lang.c++ (don't know how it got there):

>    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?  BTW, Microsoft C *does* accept this.


    I rapidly received two responses indicating that the & was the problem.
(Nobody picked on using int and char as variables :-)


    The & was "artistic" in my example.  Microsoft C does allow &string,
but provides a warning that the ampersand is ignored.

    The actual code is:


const unsigned char *chars   = "some string"
unsigned char pass[];

    c   = strchr( chars, toupper( pass[ i ]) ) - chars + 1;


    gcc would not accept this no matter what I did.

-- 
 _
Kevin D. Quitt         demott!kdq   kdq at demott.com
DeMott Electronics Co. 14707 Keswick St.   Van Nuys, CA 91405-1266
VOICE (818) 988-4975   FAX (818) 997-1190  MODEM (818) 997-4496 PEP last



More information about the Comp.lang.c mailing list