Passing Argument 1 discards qualifiers from pointer target type

Alex Nichols picture Alex Nichols · Mar 14, 2013 · Viewed 35.3k times · Source

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?

Answer

Ed S. picture Ed S. · Mar 14, 2013

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.