15

Possible Duplicate:
How to get rid of deprecated conversion from string constant to ‘char*’ warnings in GCC?

This assignment:

char *pc1 = "test string";

gives me this warning:

warning: deprecated conversion from string constant to 'char*'

while this one seems to be fine:

char *pc2 = (char*)("test string");

Is this one a really better way to proceed?

Notes: for other reasons I cannot use a const char*.

Community
  • 1
  • 1
Pietro M
  • 1,905
  • 3
  • 20
  • 24
  • 4
    Don't use a C++ compiler for C code. – pmg Dec 02 '11 at 12:19
  • 5
    You really should be using a `const char *` for this... – Oliver Charlesworth Dec 02 '11 at 12:20
  • 2
    What are those other reasons? – Marcelo Cantos Dec 02 '11 at 12:24
  • Which compiler? gcc-4.5.1 doesn't say a word. – Daniel Fischer Dec 02 '11 at 12:27
  • 2
    casting away the warning is really a bad idea. Just use `char const*pc1` and everything should be fine. – Jens Gustedt Dec 02 '11 at 12:41
  • 1
    You might not want to use `const char*`, but that string literal really is a `const`. You can't modify it. – David Heffernan Dec 02 '11 at 12:50
  • As it's C++ code, you could have tagged the question with c++ ... and use `std::string` instead – pmg Dec 02 '11 at 15:01
  • @OliCharlesworth, MarceloCantos, DavidHeffernan - I cannot: I must pass it to a function that needs a char*. And I cannot modify that function. – Pietro M Dec 02 '11 at 15:02
  • @pmg You are right, but this is C++ code that calls C functions. So the question is related to the C part of C++. – Pietro M Dec 02 '11 at 15:05
  • @DanielFischer The compiler I am forced to use is gcc 3.4.5 – Pietro M Dec 02 '11 at 15:14
  • Note to everyone saying 'don't do it': imagine a C++ project calling an old C library which is not const-correct. The API says that a parameter `char*`, but existing code assigns string literals to it.We know that this has worked for the past 20 years, so can infer that the C library never attempted to change those read-only literals. How do you proceed with new C++ code using the library? Answer: cast away the constness, get on with the real problems. – EML Oct 09 '22 at 15:20

3 Answers3

12

A string literal is a const char[] in C++, and may be stored in read-only memory so your program will crash if you try to modify it. Pointing a non-const pointer at it is a bad idea.

Wyzard
  • 33,849
  • 3
  • 67
  • 87
8

In your second example, you must make sure that you don't attempt to modify the the string pointed to by pc2.

If you do need to modify the string, there are several alternatives:

  1. Make a dynamically-allocated copy of the literal (don't forget to free() it when done):

    char *pc3 = strdup("test string"); /* or malloc() + strcpy() */

  2. Use an array instead of a pointer:

    char pc4[] = "test string";

NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • 1
    +1: Although you should point out that `strdup` is not standard C... – Oliver Charlesworth Dec 02 '11 at 12:25
  • I used `char pc4[] = "test string";` and I get no warnings. I wanted to know if this is the correct way to proceed, or if I am just planting a bug in my code... – Pietro M Dec 02 '11 at 15:24
  • @PietroM: No, you're not planting any bugs, as long as you bear in mind that the allocated array is `strlen("test string")+1` bytes long. If you need a longer array, you can specify the length explicitly: `char pc4[32] = "test string";` – NPE Dec 02 '11 at 15:28
  • To be more precise, I used `static char pc4[] = "test string";` and I get no warnings. I did it static because it is inside a function, and I want the string to be available even when out of the function. I wanted to know if this is the correct way to proceed, or if I am just planting a bug in my code... – Pietro M Dec 02 '11 at 15:33
7

That depends on whether you need to modify the string literal or not. If yes,

char pc1[] = "test string";
Mahesh
  • 34,573
  • 20
  • 89
  • 115