Bug 18741 - cannot convert `const char *' to `char *'
cannot convert `const char *' to `char *'
Product: Red Hat Linux
Classification: Retired
Component: gcc (Show other bugs)
All Linux
high Severity high
: ---
: ---
Assigned To: Jakub Jelinek
David Lawrence
Depends On:
  Show dependency treegraph
Reported: 2000-10-09 16:31 EDT by Dan Melchione
Modified: 2005-10-31 17:00 EST (History)
1 user (show)

See Also:
Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of:
Last Closed: 2000-10-09 22:47:16 EDT
Type: ---
Regression: ---
Mount Type: ---
Documentation: ---
Verified Versions:
Category: ---
oVirt Team: ---
RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: ---

Attachments (Terms of Use)
sample source (154 bytes, text/plain)
2000-10-09 17:12 EDT, Dan Melchione
no flags Details

  None (edit)
Description Dan Melchione 2000-10-09 16:31:43 EDT
#include "stdio.h"
void test(char *){}
int main(int argc, char* argv[])
    int i = 1;
    return 0;

returns the error:
test.cpp: In function `int main (int, char **)':
test.cpp:7: cannot convert `const char *' to `char *' for argument `1' 
to `test (char *)'

kgcc and other compilers work fine.  even the first statement works fine.
Comment 1 Jakub Jelinek 2000-10-09 17:11:07 EDT
That code is buggy, you should pass string constants to
const char * function arguments, not char *, because unless
-fwriteable-strings, test won't be able to write into that string.
So, either change test to test(const char *), or, if you make
yourself sure that test never modifies the string and don't want
to leave that checking on g++, you could call test((char *)"test");
g++ 2.96 is much more picky about C++ standard compliance, so it will
reject more invalid inputs than it used to in the past.
Comment 2 Dan Melchione 2000-10-09 17:12:50 EDT
Created attachment 3945 [details]
sample source
Comment 3 Dan Melchione 2000-10-09 17:15:43 EDT
If this is true, why does the first statement work?  Either way its a bug, 
either the first test, or the second test.
Comment 4 Jakub Jelinek 2000-10-09 17:31:41 EDT
I don't have C++ standard with me here at home, so I cannot tell
exactly why it works that way, but already g++ 2.95 refuses your
testcase (and only in the second test invocation, not first).
Comment 5 Michael Schwendt 2000-10-09 22:47:13 EDT
IMO, it is a compiler bug because there is no difference between the string
literals involved in both calls of test().

Implicit conversion of a const char* to non-const char* is deprecated. It still
works in order to not break old C++ code from times when string literals where
(non-const) pointers to char. But code like this test case ought not be written.
Its result at runtime is undefined. Watch this:

#include <stdio.h>

void test(char* s)
    s[0] = 'f';  // uh oh!

int main(int argc, char* argv[])
    int i = 1;
    return 0;

Segmentation fault (core dumped)
Comment 6 Jakub Jelinek 2000-10-20 10:12:54 EDT
It is actually not a bug, just a permissive g++ in the first case.
Really, if g++ would not be permissive, you would not be able to compile most
of the broken C++ code out there (because passing string constants to char *
arguments is very common).
I believe it will be changed in GCC4 or something, after some period of
just warning about it.
Comment 7 Michael Schwendt 2000-10-20 10:23:47 EDT
Nevertheless, the compiler ought to allow the second test case in the first code
example. It ought to perform implicit conversion of const char* to non-const
char* in this line:

Comment 8 Michael Schwendt 2000-10-20 10:32:17 EDT
$ g++ -Wwrite-strings test.cpp -c
test.cpp: In function `int main (int, char **)':
test.cpp:10: warning: deprecated conversion from string constant to  `char *'
test.cpp:12: cannot convert `const char *' to `char *' for argument  `1' to
`test (char *)'

Either warning or error. But both does not make sense.

Note You need to log in before you can comment on or make changes to this bug.