Subject: bug#40671: [DOC] modify literal objects

From: Eli Zaretskii <[email-masked]>
Date: Sun, 26 Apr 2020 17:19:28 +0300

> From: Dmitry Gutov <[email-masked]>
> Date: Sun, 26 Apr 2020 17:03:38 +0300
> Cc: Michael Heerdegen <[email-masked]>, [email-masked],
>  [email-masked]
> 
> > #include <string.h>
> > int main (void) { return !strcpy ("a", "b"); }
> > 
> > This function attempts to modify the "a" string constant, so it might dump core,
> > or might return 0, or might do other things.
> 
> g++ string_const.c++
> string_const.c++: In function ‘int main()’:
> string_const.c++:2:35: warning: ISO C++ forbids converting a string 
> constant to ‘char*’ [-Wwrite-strings]
>      2 | int main (void) { return !strcpy ("a", "b"); }

Did you try compiling that as a C program, not a C++ program?

If I force the C compiler to use -Wwrite-strings, then I get:

  gcc -Wwrite-strings string_const.c
  string_const.c: In function 'main':
  string_const.c:2:35: warning: passing argument 1 of 'strcpy' discards 'const' qualifier from pointer target type [-Wdiscarded-qualifiers]
   int main (void) { return !strcpy ("a", "b"); }
				     ^~~
  In file included from string_const.c:1:0:
  d:\usr\include\string.h:79:40: note: expected 'char *' but argument is of type 'const char *'
   _CRTIMP __cdecl __MINGW_NOTHROW  char *strcpy (char *, const char *);
					  ^~~~~~

but even that goes away if I modify the program as follows:

  #include <string.h>
  int main (void) { return !strcpy ((char *)"a", "b"); }