[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: char vs. unsigned char
From: |
Richard Frith-Macdonald |
Subject: |
Re: char vs. unsigned char |
Date: |
Fri, 13 Mar 2015 08:20:38 +0000 |
On 12 Mar 2015, at 19:11, Riccardo Mottola <address@hidden> wrote:
>
> Hi,
>
> a developer question. I got LuserNET working again! another application
> almost saved from bitrot :)
>
> I get however a lot of warnings about signedess in char*. In several palces
> data buffers are specified as unsigned char, while in other places data
> coming from those buffers is used as char*, including when passing to C
> functions like atoi().
>
> My simple knowledge would say that usually one can just use char* when
> treating the content as binary or as string. While signeddess matters when
> numbers are stored.
>
> I converted many buffers and structs. Everything continues to work as far as
> I can see.
>
> Could someone comment? check my patch at least by skimming over it?
>
> I wonder why these were declared as unsigned as first.
Well, my take on that is that unsigned is better/safer because a lot of code
works with integers ... and if you are using an 8bit characterset (basically
anything other than ascii), then you run the risk of implicit sign extension by
the compiler causing confusion if you are using a signed 8bit character.
I don't think I've ever seen any character/text manipulation code where use of
a signed char provides any benefit.
That makes me think that the existence of signed char is really a historical
mistake in the language.
So my preference is to use unsigned char for all my code, and simply cast
pointers to be (char*) or (const char*) when passing them to library routines
which are declared to expect trhose types.