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?

 
 
Ben Fitzgerald
Guest
Posts: n/a
 
      06-27-2003
On 27 Jun 2003 13:03:47 GMT, Dan Pop <> wrote:
> In <> Ben Fitzgerald <> writes:
>
>>it works fine, of course, but I just got lint recently
>>(actually splint on linux) and it complains:
>>
>> Assignment of int to char: x[i++] = c
>>

> No, it is not the correct and portable approach, but for another reason
> than the lint complaint.
>
> You're assigning a value in the range 0..UCHAR_MAX to a variable that can
> only represent the CHAR_MIN..CHAR_MAX range. Although this happens to
> work most of the time on most of the platforms, it is not guaranteed by
> the language to do the right thing.
>
> 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;


Okay, help me out here. If I assign c straight to x[i++]:
x[i++] = c;

I'm incorrect because c could contain a vaule greater than
UCHAR_MAX, but x can only hold CHAR_MIN...CHAR_MAX

*But*, if you have (c = getchar()) != EOF) as a test in your
while loop and getchar() "returns an unsigned char cast
to an int, or EOF" (I got this quote from a linux man page!)
then EOF cannot be assigned inside the loop thanks to the test
so the former (unsigned char) will be assigned to x[i++]

i.e.
int c, i = 0;
unsigned char x[20];

while ((c=getchar()) != EOF && i < sizeof x - 1)
x[i++] = c;

Therefore, shouldn't array x simply be of type unsigned char!?
I am agreeing with you in a roundabout way, just without the
pointers, I think.

What do you reckon?


--
Ben Fitzgerald
London, UK
 
Reply With Quote
 
 
 
 
Micah Cowan
Guest
Posts: n/a
 
      06-28-2003
Ben Fitzgerald <> writes:

> On 27 Jun 2003 13:03:47 GMT, Dan Pop <> wrote:
> > In <> Ben Fitzgerald <> writes:
> >
> >>it works fine, of course, but I just got lint recently
> >>(actually splint on linux) and it complains:
> >>
> >> Assignment of int to char: x[i++] = c
> >>

> > No, it is not the correct and portable approach, but for another reason
> > than the lint complaint.
> >
> > You're assigning a value in the range 0..UCHAR_MAX to a variable that can
> > only represent the CHAR_MIN..CHAR_MAX range. Although this happens to
> > work most of the time on most of the platforms, it is not guaranteed by
> > the language to do the right thing.
> >
> > 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;

>
> Okay, help me out here. If I assign c straight to x[i++]:
> x[i++] = c;
>
> I'm incorrect because c could contain a vaule greater than
> UCHAR_MAX, but x can only hold CHAR_MIN...CHAR_MAX


No, c will either hold a value from 0 to UCHAR_MAX (not greater), or
it will hold the value EOF.

> *But*, if you have (c = getchar()) != EOF) as a test in your
> while loop and getchar() "returns an unsigned char cast
> to an int, or EOF" (I got this quote from a linux man page!)
> then EOF cannot be assigned inside the loop thanks to the test
> so the former (unsigned char) will be assigned to x[i++]


Right, but that's the problem: assigning an unsigned char to a char
can overflow when char is a signed type.

If you are expecting that your file is made up entirely of textual
characters which are all representable in the char type, then you can
perform a check such as:

if (c > CHAR_MAX) {
/* c can't be represented as a regular char.
handle this as an error... */
}

Otherwise, if you want these bytes to be represented exactly, you
should follow Dan's advice.

>
> i.e.
> int c, i = 0;
> unsigned char x[20];
>
> while ((c=getchar()) != EOF && i < sizeof x - 1)
> x[i++] = c;
>
> Therefore, shouldn't array x simply be of type unsigned char!?
> I am agreeing with you in a roundabout way, just without the
> pointers, I think.


But, as Dan pointed out, you would then have difficulties using x with
the Standard Library's string manipulation facilities.

-Micah
 
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? Chris Torek C Programming 1 06-30-2003 12:25 PM
Re: getchar returns int, assign to array of char? Ben Fitzgerald C Programming 9 06-27-2003 07:04 PM



Advertisments