Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy is passing a string literal into a char* argument only sometimes a compiler error?
    primarykey
    data
    text
    <p>I'm working in a C, and C++ program. We used to be compiling without the make-strings-writable option. But that was getting a bunch of warnings, so I turned it off.</p> <p>Then I got a whole bunch of errors of the form "Cannot convert const char* to char* in argmuent 3 of function foo". So, I went through and made a whole lot of changes to fix those. </p> <p>However, today, the program CRASHED because the literal "" was getting passed into a function that was expecting a char*, and was setting the 0th character to 0. It wasn't doing anything bad, just trying to edit a constant, and crashing. </p> <p>My question is, why wasn't that a compiler error? </p> <p>In case it matters, this was on a mac compiled with gcc-4.0.</p> <p>EDIT: added code:</p> <pre><code>char * host = FindArgDefault("EMailLinkHost", ""); stripCRLF(linkHost, '\n'); </code></pre> <p>where:</p> <pre><code>char *FindArgDefault(char *argName, char *defVal) {// simplified char * val = defVal; return(val); } </code></pre> <p>and</p> <pre><code>void stripCRLF(char *str, char delim) { char *p, *q; for (p = q = str; *p; ++p) { if (*p == 0xd || *p == 0xa) { if (p[1] == (*p ^ 7)) ++p; if (delim == -1) *p = delim; } *q++ = *p; } *q = 0; // DIES HERE } </code></pre> <p>This compiled and ran until it tried to set *q to 0...</p> <p>EDIT 2:</p> <p>Most people seem to be missing the point of my question. I know why char foo[] = "bar" works. I know why char * foo = "bar"; doesn't work. </p> <p>My question is mostly with respect to passing parameters. One thing that occures to me is "Is it possible that this is a C vs C++ issue?" because I have some .c files and some .cpp files, and it's quite possible that C allows it, but C++ doesn't... or vice versa...</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload