Angus
Angus

Reputation: 12631

warning: pointer targets in initialization differ in signedness

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.

Upvotes: 10

Views: 67951

Answers (4)

kdmin
kdmin

Reputation: 434

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

Upvotes: 4

Sodved
Sodved

Reputation: 8598

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

Upvotes: 9

Louis
Louis

Reputation: 427

Change unsigned char * type to const char*.

Upvotes: 2

Seth Carnegie
Seth Carnegie

Reputation: 75150

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.

Upvotes: 25

Related Questions