jingo
jingo

Reputation: 1074

Passing command line arguments to app written in C

Here I wrote a little app which is able to read command line arguments

int main (int argc, const char * argv[])
{
    int c;

    while ((c = getopt (argc, argv, "Il:o:vh?")) != -1) 
    {
        switch(c)
        {
            case 'I':
                printf("I");
                break;
        }
    }

    return 0;   
}

The problem is that when I try to compile it the compiler prints

warning: passing argument 2 of ‘getopt’ from incompatible pointer type

and program crash. What I miss ?

Upvotes: 2

Views: 544

Answers (2)

RedX
RedX

Reputation: 15184

int main (int argc, const char * argv[])

should be

//no const
int main (int argc, char * argv[])

Upvotes: 3

Fred Foo
Fred Foo

Reputation: 363818

The argv argument to main should have type char *[], not const char *[] so that it can be converted to the char *const [] that getopt expects. In fact, char *[] or equivalent is mandated by the C standard for hosted implementations.

Upvotes: 7

Related Questions