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: Tomás Ó hÉilidhe
Date: Fri, 28 Mar 2008 10:54:11 -0700 PDT
Newsgroups: comp.lang.c

Richard Heathfield:

> The object whose
> value is being retrieved for the purpose of determining the
> value to be stored is not itself being modified at all except via > the 
> assignment, and in that respect is equivalent to x = x,
> which we all know is legal.


    With less competant programmers, you'll see that they avoid
certain programming techniques and contructs because they doubt their
own competency too much. You'll see them shy away from doing things
like using pointers to iterate thru array elements in a loop. If you
listen to comp.lang.c++ for twenty minutes, they'll constantly tell
you how "dangerous" it is to be using "raw pointers".

    Now I've always been *against* this whole incompetency plea thing,
but I must admit that *this* is the individual single sole part of the
C programming language where I allow my own doubts over my own
competency to reshape the way I write code. That is to say, I'll
*always* have:

for ( ; *pc; ++pos, ++pc) *pos = strchr(ascii,*pc) - ascii + 0x20;

instead of:

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

because I don't want to risk the chance of getting it wrong. This is
the one place where I actually think I should just play it safe. And
why am I so frightful? Well I had a program one time that worked
PERFECTLY on numerous different systems until I decided to enable the
compiler optimiser. All of a sudden, the program gave different
output. Of course my first assumption was that the compiler had a
dodgy optimiser... but anyway I went thru the code -- code which I
thought had been bullet-proof -- to find the problem. Here was the
culprit:

void StrToLower(char *p)
{
    while ( *p++ = tolower( (char unsigned)*p ) );
}

I'll *never* make that mistake again.

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