comp.lang.c
[Top] [All Lists]

Re: Convert native character string to ASCII array of integers

Subject: Re: Convert native character string to ASCII array of integers
From: Philip Potter
Date: Fri, 28 Mar 2008 14:00:16 +0000
Newsgroups: comp.lang.c


Richard Heathfield wrote:
Tomás Ó hÉilidhe said:
with:

    while (*pc) *pos++ = strchr(ascii,*pc++) - ascii + ' ';

I thought there might be a sequence point violation if pos and pc
point to the same thing.. ?

It's a valid point. If there is a risk of that, then make sure that the increments occur separately:

  while (*pc)
  {
    *pos = strchr(ascii,*pc) - ascii + ASCII_BASE;
    ++pos;
    ++pc;
  }

I disagree that there is a risk of UB. If we give the object which pos and pc both point to a name "obj", then the statement in question has three effects:
obj = strchr(ascii,obj) - ascii + ' ';
pos++;
pc++;
I don't see any problem in all these effects occuring in the same expression. No object is written to and read from in the same expression except obj, and it is only read to determine the new value of obj, which is allowed.

The apparent similarity of the original statement

*pos++ = strchr(ascii,*pc++) - ascii + ' ';

to statements such as

a[i] = i++;

is purely coincidental, because in the latter, the postincrement applies to an object which is referenced elsewhere in the same expression, whereas in the former, it is not. The pointer is incremented, not the pointee.

In fact it is much more similar to

*to++ = *from++;

which is valid even if from == to.

<Prev in Thread] Current Thread [Next in Thread>
Privacy Policy