473,587 Members | 2,316 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

gcc 4 signed vs unsigned char

Hello,
I've just switched to gcc 4 and I came across a bunch of warnings that
I can't fix. Example:

#include <stdio.h>

int main()
{
signed char *p = "Hola";

return 0;
}

If I compile that file, I get:
kk.c: In function 'main':
kk.c:5: warning: pointer targets in initialization differ in signedness

Only if I remove the signed from the declaration it compiles without
errors. How can I use the signed or unsigned char?

I'm using gcc 4.0.1. The reason for asking this is because in one
program I'm always using int8_t and u_int8_t values, and both of them
are signed or unsigned, no simply 'char' variables.

Thanks in advance.

Nov 15 '05 #1
22 5591


ju********@gmai l.com wrote:
Hello,
I've just switched to gcc 4 and I came across a bunch of warnings that
I can't fix. Example:

#include <stdio.h>

int main()
{
signed char *p = "Hola";

return 0;
}

If I compile that file, I get:
kk.c: In function 'main':
kk.c:5: warning: pointer targets in initialization differ in signedness

Only if I remove the signed from the declaration it compiles without
errors. How can I use the signed or unsigned char?

I'm using gcc 4.0.1. The reason for asking this is because in one
program I'm always using int8_t and u_int8_t values, and both of them
are signed or unsigned, no simply 'char' variables.

Thanks in advance.


in gcc 3.4. it's ok!

Nov 15 '05 #2
ju********@gmai l.com wrote:
Hello,
I've just switched to gcc 4 and I came across a bunch of warnings that
I can't fix. Example:

#include <stdio.h>

int main()
{
signed char *p = "Hola";

return 0;
}

If I compile that file, I get:
kk.c: In function 'main':
kk.c:5: warning: pointer targets in initialization differ in signedness

Only if I remove the signed from the declaration it compiles without
errors. How can I use the signed or unsigned char?

I'm using gcc 4.0.1. The reason for asking this is because in one
program I'm always using int8_t and u_int8_t values, and both of them
are signed or unsigned, no simply 'char' variables.


The type char is - for historical reasons - distinct from signed char
and unsigned char and may be effectively (signedness, size, range)
either the one or the other.
So, only
char *p = "Hola";
is always correct.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 15 '05 #3
ju********@gmai l.com writes:
I've just switched to gcc 4 and I came across a bunch of warnings that
I can't fix. Example:

#include <stdio.h>

int main()
{
signed char *p = "Hola";

return 0;
}

If I compile that file, I get:
kk.c: In function 'main':
kk.c:5: warning: pointer targets in initialization differ in signedness

Only if I remove the signed from the declaration it compiles without
errors. How can I use the signed or unsigned char?

I'm using gcc 4.0.1. The reason for asking this is because in one
program I'm always using int8_t and u_int8_t values, and both of them
are signed or unsigned, no simply 'char' variables.


Why would you want to use either signed char or int8_t for a character
string? The correct declaration is

char *p = "Hola";

"Plain" char is a distinct type from both signed char and unsigned
char, though it has the same representation as one of them.

However, the warning is misleading. The initialization is illegal (a
constraint violation), but the pointer targets don't actually differ
in signedness. I've just submitted a bug report; see
<http://gcc.gnu.org/bugzilla/show_bug.cgi?id =23087>.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #4
Me
ju********@gmai l.com wrote:
Hello,
I've just switched to gcc 4 and I came across a bunch of warnings that
I can't fix. Example:

#include <stdio.h>

int main()
{
signed char *p = "Hola";

return 0;
}

If I compile that file, I get:
kk.c: In function 'main':
kk.c:5: warning: pointer targets in initialization differ in signedness

Only if I remove the signed from the declaration it compiles without
errors.
"Hola" is a const char[5] which can be implicitly converted to const
char * and also unfortunately to just char *. What makes you think it
can also be implicitly converted to signed char *?

It's best to just think of unsigned char as the native byte type,
signed char as the corresponding signed integer, and plain char as a
separate native character type that just happens to be required to be
represented by either an unsigned char or signed char. It's unfortunate
it is this way but it's very, very unlikey to change.
How can I use the signed or unsigned char?
static signed char hola[] = "Hola";
signed char *p = hola;

Maybe you're confused why this works but your example doesn't. It's
because this is just syntax sugar for:

static signed char hola[5] = { 'H', 'o', 'l', 'a', '\0' };
signed char *p = hola;

(you can also get rid of the static and/or add a const qualifier. I
don't know how your actual code looks like from this small snippet)
I'm using gcc 4.0.1. The reason for asking this is because in one
program I'm always using int8_t and u_int8_t values, and both of them
are signed or unsigned, no simply 'char' variables.


who said int8_t and uint8_t must be typedeffed to a character type in
the first place? Consider it a good thing you get this warning (it
would be better if it was an error instead).

Nov 15 '05 #5


Me wrote:
ju********@gmai l.com wrote:
Hello,
I've just switched to gcc 4 and I came across a bunch of warnings that
I can't fix. Example:

#include <stdio.h>

int main()
{
signed char *p = "Hola";

return 0;
}

If I compile that file, I get:
kk.c: In function 'main':
kk.c:5: warning: pointer targets in initialization differ in signedness

Only if I remove the signed from the declaration it compiles without
errors.

"Hola" is a const char[5] which can be implicitly converted to const
char * and also unfortunately to just char *.


Almost, but not quite. "Hola" is a char[5] that decays
to char* in most circumstances. Neither the array nor the
pointer is const-qualified, even though it is forbidden to
try to modify the array.

--
Er*********@sun .com

Nov 15 '05 #6
ju********@gmai l.com wrote:
Hello,
I've just switched to gcc 4 and I came across a bunch of warnings that
I can't fix. Example:

#include <stdio.h>

int main()
{
signed char *p = "Hola";

return 0;
}

If I compile that file, I get:
kk.c: In function 'main':
kk.c:5: warning: pointer targets in initialization differ in signedness

Only if I remove the signed from the declaration it compiles without
errors. How can I use the signed or unsigned char?
You should write code that works just as well whether plain char
is signed or not.
I'm using gcc 4.0.1. The reason for asking this is because in one
program I'm always using int8_t and u_int8_t values, and both of them
are signed or unsigned, no simply 'char' variables.


You could avoid the warning with:
signed char *p = (signed char *)"Hola";

However I can't think of a reason why you would prefer this
to:
char const *p = "Hola";

Can you post some code that demonstrates why you need to
point to non-const, signed chars?

Nov 15 '05 #7
Eric Sosman <er*********@su n.com> writes:
Me wrote:

[...]
"Hola" is a const char[5] which can be implicitly converted to const
char * and also unfortunately to just char *.


Almost, but not quite. "Hola" is a char[5] that decays
to char* in most circumstances. Neither the array nor the
pointer is const-qualified, even though it is forbidden to
try to modify the array.


"Forbidden" in the sense that attempting to do so invokes undefined
behavior. For some implementations , this is indistinguishab le from it
being allowed.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #8
Keith Thompson wrote:
ju********@gmai l.com writes:

int main()
{
signed char *p = "Hola";
return 0;
}

If I compile that file, I get:
kk.c: In function 'main':
kk.c:5: warning: pointer targets in initialization differ in signedness


However, the warning is misleading. The initialization is illegal (a
constraint violation), but the pointer targets don't actually differ
in signedness. I've just submitted a bug report...


I, for one, am happy with the warning issued: "definitely signed"
is different to "maybe signed".

Plus the OP didn't indicate whether his implementation has plain
char signed or not, and if plain char were unsigned, then the
warning is definitely correct. GCC has a switch for that, and I
also think it defaults to unsigned on appropriate platforms
(eg. ARM).

Nov 15 '05 #9
"Old Wolf" <ol*****@inspir e.net.nz> writes:
Keith Thompson wrote:
ju********@gmai l.com writes:
>
> int main()
> {
> signed char *p = "Hola";
> return 0;
> }
>
> If I compile that file, I get:
> kk.c: In function 'main':
> kk.c:5: warning: pointer targets in initialization differ in signedness
>
However, the warning is misleading. The initialization is illegal (a
constraint violation), but the pointer targets don't actually differ
in signedness. I've just submitted a bug report...


I, for one, am happy with the warning issued: "definitely signed"
is different to "maybe signed".


But "signed though it's not required to be" and "signed because it's
required to be" don't "differ in signedness". The message should say
that they differ in type.

If I saw that message and didn't know that plain char is signed, I
would naturally assume that the compiler is complaining because plain
char is unsigned. If I found that plain char really is unsigned, I'd
submit a bug report against the compiler. (In fact, I did!)
Plus the OP didn't indicate whether his implementation has plain
char signed or not, and if plain char were unsigned, then the
warning is definitely correct. GCC has a switch for that, and I
also think it defaults to unsigned on appropriate platforms
(eg. ARM).


The warning wouldn't be as incorrect if plain char were unsigned, but
it would still be misleading. The initialization is illegal because
the pointer types are incompatible (and the pointer types are
incompatible because the target types are distinct types), *not*
just because the target types differ in signedness.

gcc 4.0.0 produces the warning "pointer targets in initialization
differ in signedness" for both of the following lines:

signed char *ps = "signed?";
unsigned char *pu = "unsigned?" ;

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
6465
by: MiniDisc_2k2 | last post by:
Okay, here's a question about the standard. What does it say about unsigned/signed mismatches in a comparison statement: char a = 3; unsigned char b = 255; if (a<b) Now what's the real answer here? If a is converted to unsigned, then b>a. But, if b is converted to signed,then a>b. What's the correct coversion (what is the compiler...
3
31498
by: Siemel Naran | last post by:
Hi. Is there a way to convert the type signed int to the type unsigned int, char to unsigned char, signed char to unsigned char, and so on for all the fundamental integer types? Something like template <> struct to_unsigned<signed int> : public std::unary_function<signed int, unsigned int> { unsigned int operator()(signed int x) const {...
19
4651
by: Christopher Benson-Manica | last post by:
Given signed char str_a="Hello, world!\n"; unsigned char str_b="Hello, world!\n"; what is the difference, if any, between the following two statements? printf( "%s", str_a ); printf( "%s", str_b );
9
4164
by: dam_fool_2003 | last post by:
For int data type the default range starts from signed to unsigned. If we don't want negative value we can force an unsigned value. The same goes for long also. But I don't understand why we have signed char which is -256. Does it means that we can assign the same ASCII value to both signed and unsigned. That means the ASCII value can be...
10
15633
by: tinesan | last post by:
Hello fellow C programmers, I'm just learning to program with C, and I'm wondering what the difference between signed and unsigned char is. To me there seems to be no difference, and the standard doesn't even care what a normal char is (because signed and unsigned have equal behavior). For example if someone does this: unsigned char a...
8
4056
by: Marcin Kalicinski | last post by:
Are 3 types: signed char, char and unsigned char distinct? My compiler is treating char as signed char (i.e. it has sign, and range from -128 to 127), but the following code does not call f<char> as I would expect: template<class T> void f(T t) { } template<> void f<char>(char t) {
11
2662
by: Frederick Gotham | last post by:
I'd like to discuss the use of signed integers types where unsigned integers types would suffice. A common example would be: #include <cassert> #include <cstddef> int CountOccurrences(unsigned char const val, unsigned char const p,
7
5035
by: somenath | last post by:
Hi All, I am trying to undestand "Type Conversions" from K&R book.I am not able to understand the bellow mentioned text "Conversion rules are more complicated when unsigned operands are involved. The problem is that comparisons between signed and unsigned values are machine- dependent, because they depend on the sizes of the various...
6
6441
by: Kislay | last post by:
Consider the following code snippet unsigned int i=10; int j= - 2; // minus 2 if(i>j) cout<<"i is greater"; else cout<<"j is greater"; Since i is unsigned , j is greater . I know why , but vaguely . Can
0
7843
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8205
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8339
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8220
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6619
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5392
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3840
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3872
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1185
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.