GNU bug report logs -
#40671
[DOC] modify literal objects
Previous Next
Full log
View this message in rfc822 format
> From: Dmitry Gutov <dgutov <at> yandex.ru>
> Date: Sun, 26 Apr 2020 17:03:38 +0300
> Cc: Michael Heerdegen <michael_heerdegen <at> web.de>, ke.vigouroux <at> laposte.net,
> 40671 <at> 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"); }
This bug report was last modified 5 years and 2 days ago.
Previous Next
GNU bug tracking system
Copyright (C) 1999 Darren O. Benham,
1997,2003 nCipher Corporation Ltd,
1994-97 Ian Jackson.