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: Richard Heathfield
Date: Fri, 28 Mar 2008 13:32:06 +0000
Newsgroups: comp.lang.c

Tomás Ó hÉilidhe said:

<snip>
> 
> int main(void)
> {
>     char hello[] = "hello";
> 
>     MakeASCII(hello,hello);
<snip>
> I wasn't sure whether I was able to replace:
> 
>     for ( ; *pc; ++pos, ++pc)
>        *pos = strchr(ascii,*pc) - ascii + ' ';

Don't add ' ' if you really want to add 32 and may be running on a 
non-ASCII system! Add 32 instead (or ASCII_BASE, #defined to 32, or 
something like that). Also, be absolutely sure that there is no 
possibility of strchr(ascii, *pc) returning NULL!

> 
> 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;
  }

-- 
Richard Heathfield <http://www.cpax.org.uk>
Email: -                                www">http://www. +rjh@
Google users: <                                www.cpax.org.uk/prg/writings/googly.php">http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999

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