Page 1 of 2
char & UBYTE
Posted: Wed Nov 09, 2011 5:37 pm
by JosDuchIt
Are char and UBYTE interchangeable types ?
- if no what is the difference?
- if yes how get rid of those warnings complaining about signedness? (in an easy way)
Re: char & UBYTE
Posted: Wed Nov 09, 2011 7:35 pm
by Raziel
Oooh, i wanna know too

Re: char & UBYTE
Posted: Thu Nov 10, 2011 1:11 am
by chris
exec/types.h tells you all you need to know:
Code: Select all
typedef unsigned char UBYTE; /* unsigned 8-bit quantity */
Re: char & UBYTE
Posted: Thu Nov 10, 2011 10:09 am
by salass00
JosDuchIt wrote:Are char and UBYTE interchangeable types ?
- if no what is the difference?
UBYTE is always unsigned while char without signed or unsigned keyword can be either signed or unsigned depending on the compiler, the target system and what compiler options are used (f.e. gcc allows to force either signed or unsigned with -fsigned-char and -funsigned-char options).
JosDuchIt wrote:- if yes how get rid of those warnings complaining about signedness? (in an easy way)
Use casts.
Re: char & UBYTE
Posted: Thu Nov 10, 2011 3:26 pm
by JosDuchIt
@salass00
gcc allows to force either signed or unsigned with -fsigned-char and -funsigned-char options).
i'lll try that first as easy solution. There must be hundreds of signedness complaints in the source i am working on (gcview - btw it compiles and works allready better than the 68k version under OS4) so casts were in my mind the hard solution. I guess i'll use the gcc option giving me the least signedness complaints and standardise(using casts) on that definition of char.
Re: char & UBYTE
Posted: Mon Nov 14, 2011 10:25 pm
by JosDuchIt
@salass00
Using no -f option i have 203 signedness warnings
using -fsigned-char i have 384 signedness warnings
using -funsigned-char i have 203 signedness warnings
even using casts i don't get rid of the warnings notably in the example below in the 2 stccpy calls
i always have the warning
/// pointer targets in passing argument 1&2 of 'stccpy' differ in signedness
Code: Select all
struct Picture // every picture gets one ///was Picture
{
UBYTE path[260];
UBYTE alias[40]; // the given name of this picture
}
struct Picture *getpic (char *name, char *alias, struct Base *bs, SHORT mode)
{
struct Picture *pic, *bpic;
stccpy (pic->path, name, 255);
stccpy (pic->alias, strupr(alias), 35);
}
size_t stccpy (
char * dest,
const char * src,
size_t n)
{
char * ptr = dest;
while (n>1 && *src)
{
*ptr = *src;
ptr ++;
src ++;
n--;
}
*ptr++ = '\0';
return (ptr-dest);
}
Tests :
1) without -f
2) - with -funsigned-char
3) no -f
but with the casts
Code: Select all
stccpy (pic->path, (UBYTE *)name, 255);
stccpy (pic->alias, (UBYTE *)strupr(alias), 35);
I expected to get rid of the warning both in the case 2) and 3) ??
Re: char & UBYTE
Posted: Tue Nov 15, 2011 12:15 am
by YesCop
Hi JosDuchIt,
I don't understand why you are using this code.
First, The struct Picture seems to be two filenames (path and alias ?), so why don't you use char path[260] or better char * and initialize during execution time.
Second, in getpic function, the pointer pic is not initialized so you must not believe in the results (if your code doesn't crash).
Third, you use a custom copy of one arry to another, why don't you use str???? functions in <string.h>.
Fourth, your cast can't work if you don't change the api of stccpy like stccpy (UBYTE*, UBYTE*, size_t).
YesCop
Re: char & UBYTE
Posted: Tue Nov 15, 2011 7:02 am
by thomasrapp
JosDuchIt wrote:but with the casts
Code: Select all
stccpy (pic->path, (UBYTE *)name, 255);
stccpy (pic->alias, (UBYTE *)strupr(alias), 35);
I expected to get rid of the warning both in the case 2) and 3) ??
A cast changes the type of the pointer, not the type of the function argument. If you have a pointer to UBYTE and a function which needs char, you need to cast to char, not the other way around.
The right casts in your example were
Code: Select all
stccpy ((char *)pic->path, name, 255);
stccpy ((char *)pic->alias, strupr(alias), 35);
Re: char & UBYTE
Posted: Tue Nov 15, 2011 10:49 am
by salass00
JosDuchIt wrote:
Code: Select all
struct Picture // every picture gets one ///was Picture
{
UBYTE path[260];
UBYTE alias[40]; // the given name of this picture
}
Better use TEXT type here instead of UBYTE. For string pointers you should use STRPTR (CONST_STRPTR for a constant string).
Anyway if it's too annoying to fix all the pointer signedness warnings you can always disable them with "-Wno-pointer-sign".
Re: char & UBYTE
Posted: Tue Nov 15, 2011 1:35 pm
by JosDuchIt
@yescop
as well the struct Picture as the getpic function were not completely shown.
Just what was enough to illustrate the problem.
The source compiles and the program (gcview) work
@salass00
thanks
I had to complete the casts to get rid of the warning concerning the 2d argument
Code: Select all
stccpy ((char *)pic->path, (const char *)name, 255);
stccpy ((char *)pic->alias, (const char *)strupr(alias), 35);