Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

warning: pointer targets in initialization differ in signedness

Tags:

c

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.

like image 779
Angus Avatar asked Dec 09 '11 05:12

Angus


3 Answers

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.

like image 64
Seth Carnegie Avatar answered Nov 16 '22 02:11

Seth Carnegie


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

like image 24
Sodved Avatar answered Nov 16 '22 02:11

Sodved


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".

like image 44
kdmin Avatar answered Nov 16 '22 04:11

kdmin