Bug 18741 - cannot convert `const char *' to `char *'
Summary: cannot convert `const char *' to `char *'
Keywords:
Status: CLOSED NOTABUG
Alias: None
Product: Red Hat Linux
Classification: Retired
Component: gcc
Version: 7.0
Hardware: All
OS: Linux
high
high
Target Milestone: ---
Assignee: Jakub Jelinek
QA Contact: David Lawrence
URL:
Whiteboard:
Depends On:
Blocks:
TreeView+ depends on / blocked
 
Reported: 2000-10-09 20:31 UTC by Dan Melchione
Modified: 2005-10-31 22:00 UTC (History)
1 user (show)

Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Clone Of:
Environment:
Last Closed: 2000-10-10 02:47:16 UTC
Embargoed:


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

Description Dan Melchione 2000-10-09 20:31:43 UTC
#include "stdio.h"
void test(char *){}
int main(int argc, char* argv[])
{
    test("test1");
    int i = 1;
    test(i?"test2":"test3");
    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 21:11:07 UTC
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 21:12:50 UTC
Created attachment 3945 [details]
sample source

Comment 3 Dan Melchione 2000-10-09 21:15:43 UTC
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 21:31:41 UTC
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-10 02:47:13 UTC
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[])
{
    test("test1");
    int i = 1;
    //test(i?"test2":"test3");
    return 0;
}

Segmentation fault (core dumped)


Comment 6 Jakub Jelinek 2000-10-20 14:12:54 UTC
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 14:23:47 UTC
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:

  test(i?"test2":"test3");

Comment 8 Michael Schwendt 2000-10-20 14:32:17 UTC
$ 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.