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?

mort
  • 12,988
  • 14
  • 52
  • 97
Alex Nichols
  • 876
  • 5
  • 12
  • 23
  • 6
    You say that `huffenc` takes a `char *`, but you're passing it a `char const *`... – Oliver Charlesworth Mar 13 '13 at 23:37
  • Really? Does this mean I have to use type coercion on it? – Alex Nichols Mar 13 '13 at 23:38
  • I think you could refer to this question also http://stackoverflow.com/questions/2316387/initialization-discards-qualifiers-from-pointer-target-type – Ganesh Mar 13 '13 at 23:38
  • 1
    Just define main without the `const`. – teppic Mar 13 '13 at 23:38
  • 3
    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. – Oliver Charlesworth Mar 13 '13 at 23:39
  • 1
    That's an incorrect definition of `main` anyway, just do it right to begin with; `int main(int argc, char *argv[])`. – Ed S. Mar 13 '13 at 23:39
  • @EdS. I would not call it incorrect, you can add `const` pretty much anywhere as you want. – Kijewski Mar 13 '13 at 23:42
  • 1
    @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? – Ed S. Mar 13 '13 at 23:45
  • @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). – Kijewski Mar 13 '13 at 23:49
  • @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. – Ed S. Mar 13 '13 at 23:51
  • @EdS. acknowledged, I agree with that wording. :-) – Kijewski Mar 13 '13 at 23:52
  • @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.) – Keith Thompson Apr 22 '16 at 16:11

1 Answers1

22

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.

Ed S.
  • 122,712
  • 22
  • 185
  • 265