<p>I'm working on an exceedingly large codebase, and recently upgraded to GCC 4.3, which now triggers this warning:</p>
<blockquote>
<p>warning: deprecated conversion from string constant to ‘char*’</p>
</blockquote>
<p>Obviously, the correct way to fix this is to find every declaration like</p>
<pre><code>char *s = &quot;constant string&quot;;
</code></pre>
<p>or function call like:</p>
<pre><code>void foo(char *s);
foo(&quot;constant string&quot;);
</code></pre>
<p>and make them <code>const char</code> pointers.  However, that would mean touching 564 files, minimum, which is not a task I wish to perform at this point in time.  The problem right now is that I'm running with <a href="https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Werror" rel="nofollow noreferrer">-Werror</a>, so I need some way to stifle these warnings.  How can I do that?</p>

[{'answer_id': 59687, 'author': 'DGentry', 'author_id': 4761, 'author_profile': 'https://Stackoverflow.com/users/4761', 'pm_score': 9, 'selected': True, 'text': '<p>I believe passing <a href="https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Wno-write-strings" rel="nofollow noreferrer">-Wno-write-strings</a> to GCC will suppress this warning.</p>\n'}, {'answer_id': 59690, 'author': 'Rob Walker', 'author_id': 3631, 'author_profile': 'https://Stackoverflow.com/users/3631', 'pm_score': 6, 'selected': False, 'text': '<p>Check out GCC\'s <a href="http://gcc.gnu.org/onlinedocs/gcc/Diagnostic-Pragmas.html" rel="nofollow noreferrer">Diagnostic Pragma</a> support, and the list of <a href="http://docs.redhat.com/docs/en-US/Red_Hat_Enterprise_Linux/4/html/Using_the_GNU_Compiler_Collection/warning-options.html" rel="nofollow noreferrer">-W warning options</a>.</p>\n<p>For GCC, you can use <code>#pragma warning</code> directives like explained <a href="https://stackoverflow.com/a/8140772/2436175">here</a>.</p>\n'}, {'answer_id': 59741, 'author': 'Konrad Rudolph', 'author_id': 1968, 'author_profile': 'https://Stackoverflow.com/users/1968', 'pm_score': 5, 'selected': False, 'text': '<p>If it\'s an active code base, you might still want to upgrade the code base. Of course, performing the changes manually isn\'t feasible but I believe that this problem could be solved once and for all by one single <code>sed</code> command. I haven\'t tried it, though, so take the following with a grain of salt.</p>\n<pre class="lang-none prettyprint-override"><code>find . -exec sed -E -i .backup -n \\\n    -e \'s/char\\s*\\*\\s*(\\w+)\\s*= &quot;/char const* \\1 = &quot;/g\' {} \\;\n</code></pre>\n<p>This might not find all places (even not considering function calls) but it would alleviate the problem and make it possible to perform the few remaining changes manually.</p>\n'}, {'answer_id': 67649, 'author': 'James Antill', 'author_id': 10314, 'author_profile': 'https://Stackoverflow.com/users/10314', 'pm_score': -1, 'selected': False, 'text': "<blockquote>\n  <p>The problem right now is that I'm running with -Werror</p>\n</blockquote>\n\n<p>This is your real problem, IMO. You can try some automated ways of moving from (char *) to (const char *) but I would put money on them not just working. You will have to have a human involved for at least some of the work.\nFor the short term, just ignore the warning (but IMO leave it on, or it'll never get fixed) and just remove the -Werror.</p>\n"}, {'answer_id': 237636, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>Use the <a href="https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Wno-deprecated" rel="nofollow noreferrer">-Wno-deprecated</a> option to ignore deprecated warning messages.</p>\n'}, {'answer_id': 541079, 'author': 'BlackShift', 'author_id': 2097, 'author_profile': 'https://Stackoverflow.com/users/2097', 'pm_score': 6, 'selected': False, 'text': '<p>I had a similar problem, and I solved it like this:</p>\n<pre><code>#include &lt;string.h&gt;\n\nextern void foo(char* m);\n \nint main() {\n    // warning: deprecated conversion from string constant to ‘char*’\n    //foo(&quot;Hello&quot;);\n   \n    // no more warning\n    char msg[] = &quot;Hello&quot;;\n    foo(msg);\n}\n</code></pre>\n<p>I did not have access to foo in order to adapt it to accept <code>const char*</code>, which would be a better solution because foo did not change <code>m</code>.</p>\n'}, {'answer_id': 826858, 'author': 'BillAtHRST', 'author_id': 69158, 'author_profile': 'https://Stackoverflow.com/users/69158', 'pm_score': 0, 'selected': False, 'text': '<p>You can also create a writable string from a string constant by calling <code>strdup()</code>.</p>\n\n<p>For instance, this code generates a warning:</p>\n\n<pre><code>putenv("DEBUG=1");\n</code></pre>\n\n<p>However, the following code does not (it makes a copy of the string on the heap before passing it to <code>putenv</code>):</p>\n\n<pre><code>putenv(strdup("DEBUG=1"));\n</code></pre>\n\n<p>In this case (and perhaps in most others) turning off the warning is a bad idea -- it\'s there for a reason.  The other alternative (making all strings writable by default) is potentially inefficient.</p>\n\n<p>Listen to what the compiler is telling you!   </p>\n'}, {'answer_id': 1309789, 'author': 'vy32', 'author_id': 51167, 'author_profile': 'https://Stackoverflow.com/users/51167', 'pm_score': 5, 'selected': False, 'text': '<p>I can\'t use the compiler switch. So I have turned this:</p>\n\n<pre><code>char *setf = tigetstr("setf");\n</code></pre>\n\n<p>to this:</p>\n\n<pre><code>char *setf = tigetstr((char *)"setf");\n</code></pre>\n'}, {'answer_id': 2461387, 'author': 'alexsid', 'author_id': 295560, 'author_profile': 'https://Stackoverflow.com/users/295560', 'pm_score': 3, 'selected': False, 'text': '<p><code>Test string</code> is const string. So you can solve like this:</p>\n\n<pre><code>char str[] = "Test string";\n</code></pre>\n\n<p>or:</p>\n\n<pre><code>const char* str = "Test string";\nprintf(str);\n</code></pre>\n'}, {'answer_id': 3553118, 'author': 'shindow', 'author_id': 415712, 'author_profile': 'https://Stackoverflow.com/users/415712', 'pm_score': -1, 'selected': False, 'text': "<p>See this situation:</p>\n<pre><code>typedef struct tagPyTypeObject\n{\n    PyObject_HEAD;\n    char *name;\n    PrintFun print;\n    AddFun add;\n    HashFun hash;\n} PyTypeObject;\n\nPyTypeObject PyDict_Type=\n{\n    PyObject_HEAD_INIT(&amp;PyType_Type),\n    &quot;dict&quot;,\n    dict_print,\n    0,\n    0\n};\n</code></pre>\n<p>Watch the <em>name</em> field. Using <code>gcc</code>, it compiles without warning, but in <code>g++</code> it will. I don't know why.</p>\n"}, {'answer_id': 4500719, 'author': 'Dario', 'author_id': 550107, 'author_profile': 'https://Stackoverflow.com/users/550107', 'pm_score': 2, 'selected': False, 'text': '<p>Just use type casting:</p>\n<pre><code>(char*) &quot;test&quot;\n</code></pre>\n'}, {'answer_id': 8140772, 'author': 'EdH', 'author_id': 136087, 'author_profile': 'https://Stackoverflow.com/users/136087', 'pm_score': 5, 'selected': False, 'text': '<p>Here is how to do it inline in a file, so you don\'t have to modify your Makefile.</p>\n\n<pre><code>// gets rid of annoying "deprecated conversion from string constant blah blah" warning\n#pragma GCC diagnostic ignored "-Wwrite-strings"\n</code></pre>\n\n<p>You can then later...</p>\n\n<pre><code>#pragma GCC diagnostic pop\n</code></pre>\n'}, {'answer_id': 10584743, 'author': 'msn', 'author_id': 1393907, 'author_profile': 'https://Stackoverflow.com/users/1393907', 'pm_score': -1, 'selected': False, 'text': '<p>Re <a href="https://stackoverflow.com/questions/59670/how-to-get-rid-of-deprecated-conversion-from-string-constant-to-char-warnin/3553118#3553118">shindow\'s &quot;answer&quot;</a>:</p>\n<blockquote>\n<pre><code>PyTypeObject PyDict_Type=\n{\n    ...\n\nPyTypeObject PyDict_Type=\n{\n   PyObject_HEAD_INIT(&amp;PyType_Type),\n                      &quot;dict&quot;,\n                      dict_print,\n                      0,\n                      0\n};\n</code></pre>\n<p>Watch the <em>name</em> field. Using <code>gcc</code>, it compiles without warning, but in <code>g++</code> it will. I don\'t know why.</p>\n</blockquote>\n<p>In <em>gcc (Compiling C)</em>, <a href="https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Wno-write-strings" rel="nofollow noreferrer">-Wno-write-strings</a> is active by default.</p>\n<p>In <em>g++ (Compiling C++)</em>, <a href="https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Wwrite-strings" rel="nofollow noreferrer">-Wwrite-strings</a> is active by default</p>\n<p>This is why there is a different behaviour.</p>\n<p>For us, using macros of <code>Boost_python</code> generates such warnings.\nSo we use <em>-Wno-write-strings</em> when compiling C++ since we always use <a href="https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Werror" rel="nofollow noreferrer">-Werror</a>.</p>\n'}, {'answer_id': 10952861, 'author': 'Md. Arafat Al Mahmud', 'author_id': 1096516, 'author_profile': 'https://Stackoverflow.com/users/1096516', 'pm_score': 0, 'selected': False, 'text': '<p>Just use the <a href="https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-w" rel="nofollow noreferrer">-w</a> option for <code>g++</code>.</p>\n<p>Example:</p>\n<pre><code>g++ -w -o simple.o simple.cpp -lpthread\n</code></pre>\n<p>Remember this doesn\'t avoid deprecation. Rather, it prevents showing warning message on the terminal.</p>\n<p>Now if you really want to avoid deprecation, use the <em>const</em> keyword like this:</p>\n<pre><code>const char* s = &quot;constant string&quot;;  \n</code></pre>\n'}, {'answer_id': 16867229, 'author': 'John', 'author_id': 1735922, 'author_profile': 'https://Stackoverflow.com/users/1735922', 'pm_score': 9, 'selected': False, 'text': '<p>Any functions into which you pass string literals <code>"I am a string literal"</code> should use <code>char const *</code> as the type instead of <code>char*</code>.</p>\n\n<p>If you\'re going to fix something, fix it right.</p>\n\n<p><strong>Explanation:</strong></p>\n\n<p>You can not use string literals to initialise strings that will be modified, because they are of type <code>const char*</code>. Casting away the constness to later modify them is <a href="https://stackoverflow.com/questions/3801557/can-we-change-the-value-of-an-object-defined-with-const-through-pointers">undefined behaviour</a>, so you have to copy your <code>const char*</code> strings <code>char</code> by <code>char</code> into dynamically allocated <code>char*</code> strings in order to modify them.</p>\n\n<p><strong>Example:</strong></p>\n\n<pre><code>#include &lt;iostream&gt;\n\nvoid print(char* ch);\n\nvoid print(const char* ch) {\n    std::cout&lt;&lt;ch;\n}\n\nint main() {\n    print("Hello");\n    return 0;\n}\n</code></pre>\n'}, {'answer_id': 24758381, 'author': 'tejp124', 'author_id': 2514026, 'author_profile': 'https://Stackoverflow.com/users/2514026', 'pm_score': 2, 'selected': False, 'text': '<p>Do typecasting from constant string to char pointer i.e.</p>\n\n<pre><code>char *s = (char *) "constant string";\n</code></pre>\n'}, {'answer_id': 26194079, 'author': 'John', 'author_id': 4108715, 'author_profile': 'https://Stackoverflow.com/users/4108715', 'pm_score': 4, 'selected': False, 'text': '<p>Instead of:</p>\n\n<pre><code>void foo(char *s);\nfoo("constant string");\n</code></pre>\n\n<p>This works:</p>\n\n<pre><code>void foo(const char s[]);\nfoo("constant string");\n</code></pre>\n'}, {'answer_id': 30255068, 'author': 'appapurapu', 'author_id': 2568673, 'author_profile': 'https://Stackoverflow.com/users/2568673', 'pm_score': 4, 'selected': False, 'text': '<p>In C++, use the <code>const_cast</code> as like below</p>\n\n<pre><code>char* str = const_cast&lt;char*&gt;("Test string");\n</code></pre>\n'}, {'answer_id': 33046116, 'author': 'takataka', 'author_id': 5429122, 'author_profile': 'https://Stackoverflow.com/users/5429122', 'pm_score': 5, 'selected': False, 'text': '<p>Replace</p>\n\n<pre><code>char *str = "hello";\n</code></pre>\n\n<p>with</p>\n\n<pre><code>char *str = (char*)"hello";\n</code></pre>\n\n<p>or if you are calling in function:</p>\n\n<pre><code>foo("hello");\n</code></pre>\n\n<p>replace this with</p>\n\n<pre><code>foo((char*) "hello");\n</code></pre>\n'}, {'answer_id': 35813713, 'author': 'Micheal Morrow', 'author_id': 5133527, 'author_profile': 'https://Stackoverflow.com/users/5133527', 'pm_score': 0, 'selected': False, 'text': "<p>Picking from here and there, here comes this solution.  This compiles clean.</p>\n<pre><code>const char * timeServer[] = { &quot;pool.ntp.org&quot; }; // 0 - Worldwide \n#define WHICH_NTP            0 // Which NTP server name to use.\n...\nsendNTPpacket(const_cast&lt;char*&gt;(timeServer[WHICH_NTP])); // send an NTP packet to a server\n...\nvoid sendNTPpacket(char* address) { code }\n</code></pre>\n<p>I know there's only one item in the timeServer array.  But there could be more.  The rest were commented out for now to save memory.</p>\n"}, {'answer_id': 47925509, 'author': 'Sohrab', 'author_id': 2505235, 'author_profile': 'https://Stackoverflow.com/users/2505235', 'pm_score': 1, 'selected': False, 'text': '<p>In C++, replace:</p>\n<pre><code>char *str = &quot;hello&quot;;\n</code></pre>\n<p>with:</p>\n<pre><code>std::string str (&quot;hello&quot;);\n</code></pre>\n<p>And if you want to compare it:</p>\n<pre><code>str.compare(&quot;HALLO&quot;);\n</code></pre>\n'}, {'answer_id': 49181562, 'author': 'MyGEARStationcom', 'author_id': 5365814, 'author_profile': 'https://Stackoverflow.com/users/5365814', 'pm_score': 1, 'selected': False, 'text': '<blockquote>\n<p>I don\'t understand how to apply your solution :( – kalmanIsAGameChanger</p>\n</blockquote>\n<p>Working with an Arduino <a href="http://www.arduino.cc/en/Tutorial/Sketch" rel="nofollow noreferrer">sketch</a>, I had a function causing my warnings.</p>\n<p>Original function:</p>\n<pre><code>char StrContains(char *str, char *sfind)\n</code></pre>\n<p>To stop the warnings, I added the <em>const</em> in front of the char *str and the char *sfind.</p>\n<p>Modified:</p>\n<pre><code>char StrContains(const char *str, const char *sfind).\n</code></pre>\n<p>All warnings went away.</p>\n'}, {'answer_id': 63867734, 'author': 'Anjan Parajuli', 'author_id': 13458906, 'author_profile': 'https://Stackoverflow.com/users/13458906', 'pm_score': 0, 'selected': False, 'text': '<p>While passing <em>string constants</em> to functions, write it as:</p>\n<pre><code>void setpart(const char name[]);\n\nsetpart(&quot;Hello&quot;);\n</code></pre>\n<p>Instead of <code>const char name[]</code>, you could also write <code>const char \\*name</code>.</p>\n<p>It worked for me to remove this error:</p>\n<pre class="lang-none prettyprint-override"><code>[Warning] deprecated conversion from string constant to \'char*\' [-Wwrite-strings]\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/59670', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3830/']