Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Re: getchar returns int, assign to array of char?

Reply
Thread Tools

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

 
 
Chris Torek
Guest
Posts: n/a
 
      06-27-2003
[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.
 
Reply With Quote
 
 
 
 
Dan Pop
Guest
Posts: n/a
 
      06-30-2003
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:
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Hash#select returns an array but Hash#reject returns a hash... Srijayanth Sridhar Ruby 19 07-02-2008 12:49 PM
How to assign a returns value of a javascript function to a hiddenfield in a webpart Chander Software 0 12-20-2007 09:14 AM
createImage sometime returns null and sometime returns non-null. vizlab Java 3 10-17-2007 11:21 AM
Re: getchar returns int, assign to array of char? Ben Fitzgerald C Programming 1 06-28-2003 02:24 AM
Re: getchar returns int, assign to array of char? Ben Fitzgerald C Programming 9 06-27-2003 07:04 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57