char & UBYTE

This forum is for general developer support questions.
JosDuchIt
Posts: 291
Joined: Sun Jun 26, 2011 5:47 pm
Contact:

char & UBYTE

Post 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)
User avatar
Raziel
Posts: 1170
Joined: Sat Jun 18, 2011 4:00 pm
Location: a dying planet

Re: char & UBYTE

Post by Raziel »

Oooh, i wanna know too :-)
People are dying.
Entire ecosystems are collapsing.
We are in the beginning of a mass extinction.
And all you can talk about is money and fairytales of eternal economic growth.
How dare you!
– Greta Thunberg
chris
Posts: 562
Joined: Sat Jun 18, 2011 11:05 am
Contact:

Re: char & UBYTE

Post by chris »

exec/types.h tells you all you need to know:

Code: Select all

typedef unsigned char   UBYTE;    /* unsigned 8-bit quantity */
User avatar
salass00
AmigaOS Core Developer
AmigaOS Core Developer
Posts: 530
Joined: Sat Jun 18, 2011 3:12 pm
Location: Finland
Contact:

Re: char & UBYTE

Post 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.
JosDuchIt
Posts: 291
Joined: Sun Jun 26, 2011 5:47 pm
Contact:

Re: char & UBYTE

Post 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.
JosDuchIt
Posts: 291
Joined: Sun Jun 26, 2011 5:47 pm
Contact:

Re: char & UBYTE

Post 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) ??
User avatar
YesCop
Posts: 44
Joined: Tue Oct 25, 2011 11:45 pm
Location: Caen, France

Re: char & UBYTE

Post 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
User avatar
thomasrapp
Posts: 310
Joined: Sat Jun 18, 2011 11:22 pm

Re: char & UBYTE

Post 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); 
User avatar
salass00
AmigaOS Core Developer
AmigaOS Core Developer
Posts: 530
Joined: Sat Jun 18, 2011 3:12 pm
Location: Finland
Contact:

Re: char & UBYTE

Post 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".
JosDuchIt
Posts: 291
Joined: Sun Jun 26, 2011 5:47 pm
Contact:

Re: char & UBYTE

Post 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);
Post Reply