Re: Comment on trim string function please




CBFalconer <cbfalconer@xxxxxxxxx> wrote in message
news:4879374E.53E97D90@xxxxxxxxxxxx
Bill Reid wrote:
<badc0de4@xxxxxxxxx> wrote:
Bill Reid wrote:

... snip ...

char *remove_beg_end_non_text(char *text) {
char *beg;
size_t length;

for (beg = text; *beg != '\0'; beg++)
if (!isspace(*beg)) break;

(spaces added for legibility above)

BUG: missing cast

I've not been following why the cast is important; is this some
type of "error" that has never actually occured on the planet
Earth but MIGHT happen in the unpredictable future?

Serious bug. isspace(int) requires the integer value of an
unsigned char. Repeat, unsigned.

Repetition is not clarification. By declaration, and all available
"official" documentation, isspace(int) requires a signed integer value...

If the char type on any machine
is signed isspace can receive a negative value, and quite likely
blowup.

On my machine, "char" is "signed char" by default, at least
according to SOME of the documentation. Nothing is "blowing
up". Why so?

That's why the argument to isspace should receive an
explicit cast. The only negative value those functions can receive
is EOF.

Sure you're not conflating "strings" with "streams"?

The cast can be avoided when the integer output of getc() is
passed, because getc returns the appropriately cast value in the
first place.

How do you think the characters wound up in a string in the first
place?

But you are getting those chars from a string, and
whether a raw char is signed or unsigned is always undefined.

I appear to "getting" a signed 8-bit integer value from my string,
with no problems, yet you think it is critically important to cast
it to an unsigned integer value, for a function that takes a signed
integer value as an argument, in order to perform a look-up of
certain integer values. Nope, still not getting it...

---
William Ernest Reid



.



Relevant Pages

  • Re: tree example
    ... understand the tree example but no what I just memtioned. ... The only occurrence of the string "(char *)" in page 143 is a cast ... acknowledge that malloc and it siblings do not need this type of cast. ...
    (comp.lang.c)
  • Re: Comment on trim string function please
    ... I think you have a bug. ... but does it fly past the terminating null character? ... initial space scan) then this writes outside the string. ... ASIDE FROM THE CAST "ISSUES", ...
    (comp.lang.c)
  • Re: Request for comment on my tiny learning project: dfighterdb (mytree)
    ... case CHAR:{ ... -In most of your code I looked at, your closing braces line up with ... you cast the pointer to the appropriate type. ... The cast is unnecessary. ...
    (comp.lang.c)
  • Re: Comment on trim string function please
    ... I've not been following why the cast is important; ... Serious bug. ... unsigned char. ... If plain char happens to be unsigned ...
    (comp.lang.c)
  • Re: Problems with (Cast)
    ... >> Why I'm getting this error when I try to cast as char? ... I wouldn't expect a single-character string to cast to ... > type of obj if it still doesn't work: ...
    (microsoft.public.dotnet.languages.csharp)