T
Tim Smith
I seem to have lost my copy of the ANSI C standard. Is this code legal?
/* p points somewhere in a string that is known to contain
an 'a' at or before where p points. We want to change
this 'a' to a 'b'. */
while ( *p-- != 'a' )
;
*++p = 'b';
If the only 'a' at or before the initial place p points to is the first
character of the string, the last p-- will be doing -- on a pointer to
the first character of the string, but it is then incremented with ++
before being used.
Another example. Again, p points to a string, this time known to
contain an 'a', but this time at or after where p points. To change it
to a 'b':
--p;
while ( *++p != 'a' )
;
*p = 'b';
If p started at the beginning of the string, this would --p to before
the string, then immediately ++p back into the string.
I seem to remember that you can't take the address of elements of an
array before the 0th element, but I don't recall if using -- on a
pointer counts as taking the address of something.
/* p points somewhere in a string that is known to contain
an 'a' at or before where p points. We want to change
this 'a' to a 'b'. */
while ( *p-- != 'a' )
;
*++p = 'b';
If the only 'a' at or before the initial place p points to is the first
character of the string, the last p-- will be doing -- on a pointer to
the first character of the string, but it is then incremented with ++
before being used.
Another example. Again, p points to a string, this time known to
contain an 'a', but this time at or after where p points. To change it
to a 'b':
--p;
while ( *++p != 'a' )
;
*p = 'b';
If p started at the beginning of the string, this would --p to before
the string, then immediately ++p back into the string.
I seem to remember that you can't take the address of elements of an
array before the 0th element, but I don't recall if using -- on a
pointer counts as taking the address of something.