In article <11**********************@f14g2000cwb.googlegroups .com>,
Kobu <ko********@gmail.com> wrote:
Hello,
Wonder if the below double access to s is undefined behaviour? Works
fine, but I know "works" can still be a consequence of undefined
behaviour.
#include <string.h>
void mystrcat(char *s, const char *t)
{
/* is the below undefined behaviour ? */
s += strlen(s);
while(*s++ = *t++)
; /* empty statement */
}
S is accessed twice (which is a no no), yet the statement "feels" right
to me.
Accessing s twice isn't a problem, if none of those accesses modifies it
(consider doing "i+i") or of only one of the accesses modifies it and
all the others are required to determine the new value to be stored
(as in your code).
The restrictions on multiple access are when an attempt is made to modify
an object twice between sequence points (i=i++) or when an object is
modified and an unrelated access to it is made (which is hard to come
up with a simple example for; aliasing and multiple side-effects are
usually involved - consider a[++*i]=a[++*j], if i or j point at the
element of a[] that gets modified).
dave
--
Dave Vandervies
dj******@csclub.uwaterloo.ca
Being rude and burbly in italian gets you killfiled just as fast as in
english. Some of us are multilingual.
--Mark McIntyre in comp.lang.c