[cfe-dev] char and const char erroring out

Jeroen Ruigrok van der Werven asmodai at in-nomine.org
Tue Jul 24 10:25:32 PDT 2007

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

#include <unistd.h>

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 at 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. :)

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

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


"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

Jeroen Ruigrok van der Werven <asmodai(-at-)in-nomine.org> / asmodai
イェルーン ラウフロック ヴァン デル ウェルヴェン
http://www.in-nomine.org/ | http://www.rangaku.org/
If you would thoroughly know anything, teach it to others...

More information about the cfe-dev mailing list