bug-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug#40671: [DOC] modify literal objects


From: Eli Zaretskii
Subject: bug#40671: [DOC] modify literal objects
Date: Sun, 26 Apr 2020 17:19:28 +0300

> From: Dmitry Gutov <dgutov@yandex.ru>
> Date: Sun, 26 Apr 2020 17:03:38 +0300
> Cc: Michael Heerdegen <michael_heerdegen@web.de>, ke.vigouroux@laposte.net,
>  40671@debbugs.gnu.org
> 
> > #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"); }





reply via email to

[Prev in Thread] Current Thread [Next in Thread]