char and const char erroring out

With a lot of Unix userland utilities this is a common occurence:

#include <unistd.h>

int
main(int argc, char *argv)
{
        int ch;

        while ((ch = getopt(argc, argv, "h")) != -1) {
                switch (ch) {
                case 'h':
                        return (-1);
                }
        }

        return (0);
}

[19:14] [asmodai@nexus] (1150) {18} % clang test.c
test.c:8:28: error: incompatible types passing 'char *' to function expecting 'char *const '
        while ((ch = getopt(argc, argv, "h")) != -1) {
                     ~~~~~~ ^~~~

Well, it is correct yes, argv is a char *argv and the prototype of getopt()
is as follows:

int getopt(int argc, char * const argv, const char *optstring);

But in this case I think error is a pretty hefty one. You would not be able to
compile any of the userland tools within any BSD like this. :slight_smile:

One could argue that the main call would need to be:
int main(int argc, char * const argv)

But C99 5.1.2.2.1 says:

"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; or in some other implementation-defined manner."

and

"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."

so that solution is a no-no since it would not be compliment to the last
paragraph.