0

This bothers me. It gives me a warning of

passing argument 1 of ‘funcName’ discards qualifiers from pointer target type

however, the program to run just fine and printing the submitted value.

The functions are the following

void funcName(char *str) {
    printf("%s", str);
}

void main() {
    funcName("Hello world");
}

output is Hello world.

William Pursell
  • 204,365
  • 48
  • 270
  • 300
Sleek13
  • 69
  • 6
  • 1
    How did you get that warning? Please post the compilation line! – Iharob Al Asimi Mar 23 '17 at 22:36
  • @Sleek 13-- Your issue has to do with which compiler warnings are enabled. String literals are not `const`, but are of type `char []`. It is UB to modify a string literal, and with GCC the flag `-Wwrite-strings` enables this warning. What compiler are you using, and what is the invocation? – ad absurdum Mar 23 '17 at 23:03

2 Answers2

5

It's because "Hello, world" is constant, so change the function to

void funcName(const char *text) 
{
    printf("%s\n", text);
}

String literals are constant, they are stored in a read only memory section of your program, passing the pointer without const means that you can accidentally modify it inside the target function, if you do so, that would cause undefined behavior, and the compiler is trying to protect you from that.

Also, void main() is not a standard compliant valid signature for main(), you can find it in old books, previous to the standard, but now it's no longer accepted, accepted and standard signatures are

  • int main(void) If you don't handle command line arguments.
  • int main(int argc, char **argv) To handle argc parameteres stored in argv that where passed in the command line.
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
  • I get no warnings in GCC 4.9 with `gcc -std=c99 -Wall -Wextra -Wpedantic` for OP's original code (with fixed signature for `main()` and added `#include `). – ad absurdum Mar 23 '17 at 22:31
  • At least a warning for `void main()`! Should be an error I think if `-std=c99` is passed. – Iharob Al Asimi Mar 23 '17 at 22:33
  • @DavidBowling In fact this one `error: return type of 'main' is not 'int' [-Werror=main]`, and it's directly an error, not a warning. – Iharob Al Asimi Mar 23 '17 at 22:34
  • Yes, I get warnings for `void main()`, and the missing `#include`; I mean, when I fix those, I see no warning for discarding `const`. – ad absurdum Mar 23 '17 at 22:35
  • Adding the `-Wwrite-strings` flag triggers the warning; [this related question](http://stackoverflow.com/questions/4493139/are-string-literals-const) discusses the issue a bit, and there is a suggestion that `g++` enables this warning by default. But, I don't think there is a problem passing a string literal into a function as `char *`, even though it is UB to modify a string literal. – ad absurdum Mar 23 '17 at 22:59
2

It seems that the problem is that this C program is compiled as a C++ program.

In C++ string literals have types of constant character arrays. So if in a C++ program you supply a string literal as an argument to a function that has the corresponding parameter without the qualifier const then the compiler will issue a message.

If to compile the program as a C program then the code is valid because in C string literals have types of non-constant character arrays and the compiler should not issue a diagnostic message relative to qualifiers.

Nevertheless in any case it is better to declare the function like

void funcName( const char *str );
               ^^^^^^

because in this case the user of the function can be sure that the string passed to the function will not be changed.

Take into account that function main without parameters shall be declared in C like

int main( void )
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Despite a couple of requests, OP has not provided details of compilation. Nonetheless, I believe that this is the correct answer, and you have my UV. It seems an important detail, which you mention, that strings in C are _not_ `const`. I did try to compile OP code with g++, and got `warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]`; not exactly the warning mentioned by OP, but related. – ad absurdum Mar 24 '17 at 00:41