Passing Argument 1 discards qualifiers from pointer target type
Asked Answered
S

1

16

My main function is as follows:

int main(int argc, char const *argv[])
{
    huffenc(argv[1]);
    return 0;
}

The compiler returns the warning:

huffenc.c:76: warning: passing argument 1 of ‘huffenc’ discards qualifiers from pointer target type

For reference, huffenc takes a char* input, and the function is executed, with the sample input "senselessness" via ./huffenc senselessness

What could this warning mean?

Standoffish answered 13/3, 2013 at 23:36 Comment(12)
You say that huffenc takes a char *, but you're passing it a char const *...Urtication
Really? Does this mean I have to use type coercion on it?Standoffish
I think you could refer to this question also #2316887Gazette
Just define main without the const.Scute
Well you could, but that's inelegant (and potentially dangerous). A better solution is to figure out why huffenc needs a non-const pointer, and change it to const if possible.Urtication
That's an incorrect definition of main anyway, just do it right to begin with; int main(int argc, char *argv[]).Oriflamme
@EdS. I would not call it incorrect, you can add const pretty much anywhere as you want.Elmiraelmo
@Kay: Well, the standard is pretty clear that the definition of main is either int main(int argc, char *argv[]) or int main(void). It goes on to say that "5.1.2.2.1 Program startup: The parameters argc and argv and the strings pointed to by the argv array shall be modifiable by the program..." So, I would call it incorrect. It will work of course, but why? When have either of the two standard definitions led to buggy programs?Oriflamme
@EdS. well, I know that the program may update argv and the strings passed in argv as long as you don't violate the bounds. Either way, int main(int, const char**) may not be elegant, but it is definitely not "incorrect" (it's just the word that I don't like).Elmiraelmo
@Kay: Well, I suppose "non-standard" or "not idiomatic" may have been better, but when the standard tells you what your function signature should be... you should take its advice.Oriflamme
@EdS. acknowledged, I agree with that wording. :-)Elmiraelmo
@Kay: Section 5.1.2.2.1 of the ISO C standard specifies two standard definitions for main (quoted above by Ed S.). It doesn't say anywhere that you can arbitrarily add const, and I'm not at all sure that a conforming compiler is required to accept it if you do. If you have an argument that adding const is valid (i.e., must be accepted by any conforming hosted C compiler), I'd be interested in seeing it. (Just noticed how hold this question is; not sure why it showed up on the front page for me.)Mchugh
O
23

It means that you're passing a const argument to a function which takes a non-const argument, which is potentially bad for obvious reasons.

huffenc probably doesn't need a non-const argument, so it should take a const char*. However, your definition of main is non-standard.

The C99 standard Section 5.1.2.2.1 (Program startup) states:

The function called at program startup is named main. The implementation declares no prototype for this function. It shall be defined with a return type of int and with no parameters:

int main(void) { /* ... */ }

or with two parameters (referred to here as argc and argv, though any names may be used, as they are local to the function in which they are declared):

int main(int argc, char *argv[]) { /* ... */ }

or equivalent;9) or in some other implementation-defined manner.

And goes on to say...

...The parameters argc and argv and the strings pointed to by the argv array shall be modifiable by the program, and retain their last-stored values between program startup and program termination.

Oriflamme answered 13/3, 2013 at 23:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.