I am trying to build the M-SIM architecture simulator, but when I run the make utility, gcc reports this error (it is not even a warning)
note: expected ‘char *’ but argument is of type ‘const char *’
Since when this is considered an error. Is there any flags that can bypass this check?
This is an error because passing a
const char*argument to a function that takes achar*parameter violates const-correctness; it would allow you to modify aconstobject, which would defeat the whole purpose ofconst.For example, this C program:
produces the following compile-time diagnostics from gcc:
The final message is marked as a “note” because it refers to the (perfectly legal) declaration of
func(), explaining that that’s the parameter declaration to which the warning refers.As far as the C standard is concerned, this is a constraint violation, which means that a compiler could treat it as a fatal error. gcc, by default, just warns about it and does an implicit conversion from
const char*tochar*.When I run the program, the output is:
which shows that, even though I declared
messageasconst, the function was able to modify it.Since gcc didn’t treat this as a fatal error, there’s no need to suppress either of the diagnostic messages. It’s entirely possible that the code will work anyway (say, if the function doesn’t happen to modify anything). But warnings exist for a reason, and you or the maintainers of the M-SIM architecture simulator should probably take a look at this.
(Passing a string literal to
func()wouldn’t trigger these diagnostics, since C doesn’t treat string literals asconst. (It does make the behavior of attempting to modify a string literal undefined.) This is for historical reasons. gcc does have an option,-Wwrite-strings, that causes it to treat string literals asconst; this actually violates the C standard, but it can be a useful check.)As I mentioned in a comment, it would be helpful if you’d show us the code that triggers the diagnostics.
I even downloaded and built the M-SIM architecture simulator myself, but I didn’t see that particular message.