Pointer arithmetic

Richard Sargent rsargent at alias.UUCP
Tue Jan 8 04:37:26 AEST 1991


In article <1991Jan5.001607.5915 at demott.com> kdq at demott.COM (Kevin D. Quitt) writes:
>
>const unsigned char *chars   = "some string"
>unsigned char pass[];
>
>    c   = strchr( chars, toupper( pass[ i ]) ) - chars + 1;
>
>

One thing that comes to mind is that a *very* fussy compiler, such
as gcc with -pedantic (?) turned on, would complain that the
difference could be between strings in two different address spaces.
This would result in a meaningless difference.

Now, by definition, strchr() returns a pointer to within the string
(or NULL, I think). But, the compiler may be ignoring what the
standard says the function does, and may just consider it as any
function returning a pointer to char. In the event that the 
character is NOT in the string, a NULL return value will result
in a meaningless difference.

Checking the return value and only taking the difference when
non-NULL may be enough. Or maybe the compiler is just too fussy.
Or maybe the compiler has a bug. Or ...




More information about the Comp.lang.c mailing list