Re: getchar returns int, assign to array of char?

Discussion in 'C Programming' started by Chris Torek, Jun 27, 2003.

  1. Chris Torek

    Chris Torek Guest

    [getchar() returns a value in the range of an unsigned char, or
    EOF, but we need to stuff this into plain, possibly-signed "char"s.]

    In article <bdhfbj$m55$> Dan Pop <> wrote:
    >One idea would be declare x as array of unsigned char. But, if you want
    >to pass it to a library expecting a string, you have a problem, because
    >it expects a pointer to char as argument. So, the right solution is to
    >use a pointer to unsigned char when storing values into your array:
    >
    > int c, i = 0;
    > char x[20];
    > unsigned char *p = (unsigned char *)x;
    >
    > while((c = getchar()) != EOF && i < sizeof x - 1)
    > p[i++] = c;


    If one were using an implementation with signed 1s-complement
    or signed-magnitude "char", this might stick trap representations
    in, and would likely distort the desired values.

    Such systems have other problems, though. For the most part
    they might want to just make plain char unsigned, as long as
    this does not lead to CHAR_MAX > INT_MAX (which raises even
    more problems).

    I consider this whole area a bit of a mess, and am usually willing
    to restrict myself to saner systems. :)
    --
    In-Real-Life: Chris Torek, Wind River Systems (BSD engineering)
    Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
    email: forget about it http://67.40.109.61/torek/index.html (for the moment)
    Reading email is like searching for food in the garbage, thanks to spammers.
    Chris Torek, Jun 27, 2003
    #1
    1. Advertising

  2. Chris Torek

    Dan Pop Guest

    In <bdi8ab$pt6$> Chris Torek <> writes:

    >[getchar() returns a value in the range of an unsigned char, or
    >EOF, but we need to stuff this into plain, possibly-signed "char"s.]
    >
    >In article <bdhfbj$m55$> Dan Pop <> wrote:
    >>One idea would be declare x as array of unsigned char. But, if you want
    >>to pass it to a library expecting a string, you have a problem, because
    >>it expects a pointer to char as argument. So, the right solution is to
    >>use a pointer to unsigned char when storing values into your array:
    >>
    >> int c, i = 0;
    >> char x[20];
    >> unsigned char *p = (unsigned char *)x;
    >>
    >> while((c = getchar()) != EOF && i < sizeof x - 1)
    >> p[i++] = c;

    >
    >If one were using an implementation with signed 1s-complement
    >or signed-magnitude "char", this might stick trap representations
    >in, and would likely distort the desired values.


    Assigning values in the 0..UCHAR_MAX range to a char that happens to be
    signed has an even higher chance of doing that, considering that the
    conversion is not well defined for values above SCHAR_MAX.

    >Such systems have other problems, though. For the most part
    >they might want to just make plain char unsigned,


    They basically have to, considering the <ctype.h> issue, too.

    >as long as
    >this does not lead to CHAR_MAX > INT_MAX (which raises even
    >more problems).


    That's downright pathological for a hosted implementation: it breaks both
    <stdio.h> and <ctype.h>, whose specification implicitly assumes that the
    type int can represent the range 0..UCHAR_MAX.

    >I consider this whole area a bit of a mess, and am usually willing
    >to restrict myself to saner systems. :)


    Did anyone produce such a pathological hosted implementation?

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
    Dan Pop, Jun 30, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben Fitzgerald

    Re: getchar returns int, assign to array of char?

    Ben Fitzgerald, Jun 26, 2003, in forum: C Programming
    Replies:
    9
    Views:
    950
    Ben Fitzgerald
    Jun 27, 2003
  2. Schnoffos
    Replies:
    2
    Views:
    1,206
    Martien Verbruggen
    Jun 27, 2003
  3. Ben Fitzgerald

    Re: getchar returns int, assign to array of char?

    Ben Fitzgerald, Jun 27, 2003, in forum: C Programming
    Replies:
    1
    Views:
    6,537
    Micah Cowan
    Jun 28, 2003
  4. trey

    newbie: char* int and char *int

    trey, Sep 10, 2003, in forum: C Programming
    Replies:
    7
    Views:
    401
    Irrwahn Grausewitz
    Sep 10, 2003
  5. Hal Styli
    Replies:
    14
    Views:
    1,625
    Old Wolf
    Jan 20, 2004
Loading...

Share This Page