gcc 4 signed vs unsigned char

K

Keith Thompson

I thougth that using int8_t would be more portable. Anyway, in my code
I also use int8_t, int16_t, int32_t (and their unsigned versions)
instead of using int, short, long ... Should I use the intx_t or the
int, short, etc? My main aim is portability among platforms.

Use whatever type is appropriate to your task. If you need an 8-bit
2's-complement type with no padding bits, use int8_t (assuming you
have a C99 implementation) -- but chances are you don't really need
all those properties.

If you need a signed type that's at least 16 bits, use int_least16_t.
Or use int.

And if you need to represent characters, use char; that's what it's
for.
 
L

Lawrence Kirby

Except there's still a problem. Take a function which returns a
pointer *INTO* one of its arguments (or possibly NULL). Examples
in the C library include strchr(), strrchr(), and strstr(), but you
can imagine lots of parsing functions that do that. Now, if you
pass in a const-poisoned pointer, it needs to be const-poisoned in
the return type. If you pass in a non-const-poisoned pointer, it
needs to be not const-poisoned on the way out, so you can use it
to write on the string. I need two nearly-identical copies of every
type of this function, one const-poisoned, the other not? Yeech.
C isn't C++ with its overloading.

While you can't fix this for existing standard library functions short
of casting the return value when you need a pointer to const, you can
design your own functions to avoid this, e.g. by returning an offset
instead of a pointer. Doing this also avoids a possible need for taking
pointer differences and the range issues of ptrdiff_t.

Lawrence
 
G

Gordon Burditt

That is, prior to C89 there was no way to say "const" in a C
While you can't fix this for existing standard library functions short
of casting the return value when you need a pointer to const, you can
design your own functions to avoid this, e.g. by returning an offset
instead of a pointer. Doing this also avoids a possible need for taking
pointer differences and the range issues of ptrdiff_t.

You also have to deal with the case where the function would return
NULL. I suppose you could use offset -1 for this case. (At least
ptrdiff_t is signed). It looks ugly. I guess it would work, though.

Gordon L. Burditt
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,169
Messages
2,570,915
Members
47,456
Latest member
JavierWalp

Latest Threads

Top