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.
I think what is happening is the dereference operator is binding
"tighter" than the pointer arithmetic.
This gives the equivalent of:
printf( "%c\n", (*Aptr) + 7 );
instead of what you want:
printf( "%c\n", *(Aptr + 7) );
It looks like you need to explicitly add the parentheses so that the
pointer is adjusted before it is dereferenced.
I haven't fed my solution to a C compiler, so I'm only "pretty sure"
it's correct.
--
Tony
_______________________________________________
EUGLUG mailing list
[email protected]
http://www.euglug.org/mailman/listinfo/euglug