T
Tom Zych
Hi. I've been using code like this:
*p++ = 'x';
*p++ = *q++;
for a long time now. It works. The code below compiles with no
warnings and runs correctly. Even splint doesn't complain about
those lines.
Is this well-defined according to the standard? I've read a little
about sequence points and not doing things like "i = i++". This
would seem to be similar. Is it only a problem if you're using and
modifying the *same* variable, more than once, without an
intervening sequence point?
In particular, is
*p++ = *q++;
guaranteed to dereference p and q, and perform the assignment,
before either is incremented?
--------------------------------------------------
#include <stdio.h>
int main(void)
{
char s[] = "Some\ntext\nin\nseveral\nlines\n";
char t[100], *src, *dest;
// replace each LF with CRLF
for (src = s, dest = t; *src; ) {
if (*src == '\n')
*dest++ = '\r';
*dest++ = *src++;
}
*dest = '\0';
fputs(t, stdout);
return 0;
}
*p++ = 'x';
*p++ = *q++;
for a long time now. It works. The code below compiles with no
warnings and runs correctly. Even splint doesn't complain about
those lines.
Is this well-defined according to the standard? I've read a little
about sequence points and not doing things like "i = i++". This
would seem to be similar. Is it only a problem if you're using and
modifying the *same* variable, more than once, without an
intervening sequence point?
In particular, is
*p++ = *q++;
guaranteed to dereference p and q, and perform the assignment,
before either is incremented?
--------------------------------------------------
#include <stdio.h>
int main(void)
{
char s[] = "Some\ntext\nin\nseveral\nlines\n";
char t[100], *src, *dest;
// replace each LF with CRLF
for (src = s, dest = t; *src; ) {
if (*src == '\n')
*dest++ = '\r';
*dest++ = *src++;
}
*dest = '\0';
fputs(t, stdout);
return 0;
}