[NTLUG:Discuss] OT C question

Paul M Foster paulf at quillandmouse.com
Sat Feb 18 00:52:10 CST 2006


Fred James wrote:
> All
> #include <string.h>
> char *strchr(char *S, int c)
> returns a pointer to the first occurrence of c in S, or Null if c is not 
> found.
> If S is "123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ1"
> and c is T then given
> Aptr = strchr(S, c);
> then
> printf( "%c\n", *Aptr);
> will yield T and
> printf( "%c\n", *Aptr+1);
> will yield U, but
> printf( "%c\n", *Aptr+7);
> does not yield 1, but rather [ where the ASCII values are (begin snip)
>     | 80 P   | 81 Q   | 82 R   | 83 S   | 84 T   | 85 U   | 86 V   | 87 
> W   |
>     | 88 X   | 89 Y   | 90 Z   | 91 [   | 92 \   | 93 ]   | 94 ^   | 95 
> _   |
> (end snip)
> so the pointer doesn't seem to be to a position in S, but rather to an 
> ASCII value.
> 
> Does that seem right, or am I missing something?  Thank you in advance 
> for any help you may be able to offer.
> Regards
> Fred James
> 

The printf expressions above should be:

printf("%c\n", *(Aptr + 1));
printf("%c\n", *(Aptr + 7));

The "contents of" operator (*) binds more tightly than the addition 
operator (+), so in your code, you're just incrementing the contents of 
the pointer location, rather than incrementing the pointer.

-- 
Paul M. Foster




More information about the Discuss mailing list