Warning: pointing goals during initialization vary by subscription

My compiler (gcc) shows a warning

#include<stdio.h> struct s{ unsigned char *p; }; int main() { struct sa = {"??/??/????"}; //warning printf("%s",ap); return 0; } 

warning: pointing goals during initialization vary by subscription

please help me why this warning comes.

+6
source share
4 answers

String literals are not of type unsigned char* .

You probably wanted to print const char* in your structure. If not, you probably do not want to assign a string literal to it without making it const , because it is illegal to change the memory in which string literals are stored.

+16
source

As @Seth Carnegie said, string literals are of type char* , not unsigned char* . This way you can avoid this warning with an explicit type. i.e.

 #include<stdio.h> struct s{ unsigned char *p; }; int main() { struct sa = {(unsigned char *)"?""?/?""?/????"}; // no warning printf("%s",ap); return 0; } 

Edit: Modified string literal to remove possible trigraph

+6
source

There may be some cases where changing compiler options should be a reasonable way to solve this problem.

Example:

You have an API with some prototype like this:

 void Display (unsigned char * Text); 

and you want to call like this:

 Display ("Some text"); 

You may receive the same warning ("pointing targets when passing argument 1" Display "acquaintance differ").

This warning is related to the -Wpointer-sign flag, which, referring to the GNU compiler link, "... means -Wall and -pedantic , which can be disabled using -Wno-pointer-sign ".

+4
source

Change unsigned char * to const char *.

+1
source

Source: https://habr.com/ru/post/903430/


All Articles