My compiler(gcc) is showing the warning
#include<stdio.h>
struct s{
unsigned char *p;
};
int main() {
struct s a = {"??/??/????"}; //warning
printf("%s",a.p);
return 0;
}
warning: pointer targets in initialization differ in signedness
please help me to why is this warning comes.
String literals are not of type unsigned char*
.
You probably meant to type const char*
in your struct. If not, you probably do not want to assign a string literal to it without making it const
, because it is illegal to modify the memory in which string literals reside.
As @Seth Carnegie said, string literals are of type char*
, not unsigned char*
. So you can avoid this warning with an explicit type cast. i.e.
#include<stdio.h>
struct s{
unsigned char *p;
};
int main() {
struct s a = {(unsigned char *)"?""?/?""?/????"}; // no warning
printf("%s",a.p);
return 0;
}
Edit: Changed string literal to remove the possible trigraph
There may be some cases when change the compiler options should be a reasonable way to solve that.
An example:
You have an API with some prototype like this:
void Display (unsigned char * Text);
and you want call like this:
Display ("Some text");
You may get the same warning ("pointer targets in passing argument 1 of 'Display' differ in signedness").
This warning is due the flag -Wpointer-sign
that, quoting GNU compiler reference, "...is implied by -Wall
and by -pedantic
, which can be disabled with -Wno-pointer-sign
".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With