warning: comparison with string literal results in unspecified behavior [-Waddress]

Josef Wolf jw@raven.inka.de
Wed Oct 10 09:21:00 GMT 2018


Hello,

I have a piece of code which is called very frequently, but should output
diagnostic messages only if it had actually changed:

   #define CHECK(func, message)                    \
       if(!(func)) {                                       \
           if (DEBUG && reason!=message) {                         \
               reason = message;                                   \
               printf("invalid configuration: %s\r\n", message);   \
           }                                                       \
           return 0;                                               \
       }
   
   int valid_config (void)
   {
     static char *reason=NULL;
   
     CHECK(check_magics(), "magic numbers");
     CHECK(config_ptr_valid(configvers), "configvers ptr");
     CHECK(config_ptr_valid(configexec), "configexec ptr");
     CHECK(strncmp (configexec, COMPATVERSION, sizeof(COMPATVERSION))>=0, "compatibility");
     CHECK(!strncmp(configvers,version(),SIGNIFICANT_VERSION_LETTERS), "version mismatch");
   
     return 1;
   }

With this code, I get the warning that comparing with a string literal is
unspecified.

A search on google finds me lots of explanations about confusing pointers with
strings. But in this case, I _do_ remember the pointer to the literal and
want to do a pointer comparison. Doing strcpy/strcmp here is not an option,
since this function is called very frequently.

Is there any reason, that a pointer to a string literal can change?

-- 
Josef Wolf
jw@raven.inka.de



More information about the Gcc-help mailing list