Re: What's the deal with the "toupper" family?
- From: Andrew Poelstra <apoelstra@xxxxxxxxxxxxxx>
- Date: Thu, 06 Jul 2006 04:56:18 GMT
On 2006-07-06, Peter Nilsson <airia@xxxxxxxxxxx> wrote:
Andrew Poelstra wrote:
On 2006-07-06, Frederick Gotham <fgothamNO@xxxxxxxx> wrote:
Peter Nilsson posted:
<slightly altered>
toupper( *(unsigned char const *)&c )
Does anyone else agree with this?
It looks overly complicated to me.
In normal form, I use things like...
const unsigned char *us = (const unsigned char *) s;
for (; *us; us++) *us = toupper(*us);
No matter what you think `const' means in this context, it's wrong. You
change both `us' /and/ `*us' in the second line.
If that's too complicated for some people, so be it.
Most simple-minded people believe that the const keyword will create a
constant. It's true that we find it `too complicated' to violate that.
It's safe because an unsigned char cannot have any trap
representations, but nonetheless, does it do what we want
it to do, and is it preferable over the following?
As I said, it's up to the programmer to pass the right value.
Different circumstances may well require different forms.
Where and how you source and store the character is a
factor in deciding which method you use.
The point of the cast is to work correctly, even if the programmer passes
the wrong value. Perhaps the programmer is passing input from a file
stream or something, and doesn't want to validate the string for such
a simple function. (And perhaps the string being uppercase is required
for future validations.)
toupper( (unsigned char)c );
No; the latter is much clearer and just as functional, IMHO.
But fails for potentially conforming implementations. To many people,
that's acceptable.
Under what circumstances will casting to unsigned char fail, and how
will it fail?
--
Andrew Poelstra <http://www.wpsoftware.net/projects/>
To email me, use "apoelstra" at the above address.
"You people hate mathematics." -- James Harris
.
- Follow-Ups:
- Re: What's the deal with the "toupper" family?
- From: Peter Nilsson
- Re: What's the deal with the "toupper" family?
- References:
- What's the deal with the "toupper" family?
- From: Frederick Gotham
- Re: What's the deal with the "toupper" family?
- From: Peter Nilsson
- Re: What's the deal with the "toupper" family?
- From: Frederick Gotham
- Re: What's the deal with the "toupper" family?
- From: Andrew Poelstra
- Re: What's the deal with the "toupper" family?
- From: Peter Nilsson
- What's the deal with the "toupper" family?
- Prev by Date: Re: How to get the current date information only?
- Next by Date: Is not printf("%p", &i) correct?
- Previous by thread: Re: What's the deal with the "toupper" family?
- Next by thread: Re: What's the deal with the "toupper" family?
- Index(es):
Relevant Pages
|