Connor McFadden
Connor McFadden

Reputation: 451

Copying cmd line arguments into an array

I'm trying to copy a command line argument into an array in C. For example, if I entered ./rpd 5 6 3 then I'd have an array of {5, 6, 3}.

My code is:

int main(int argc) {

    int numberInQueue;
    char *queueOfClients;
    int i;

    queueOfClients = malloc(sizeof(char*) * argc);

    for(i = 0; i <= argc; i++) {
        queueOfClients[i] = malloc(strlen(*(argc + i)) * sizeof(char));
    }
}

The error I seem to be getting is:

error: invalid type argument of unary '*' (have 'int')

How can I resolve this error?

Upvotes: 0

Views: 1835

Answers (2)

SzG
SzG

Reputation: 12619

C comes with this array by default. Your main() should look like this:

int main(int argc, char *argv[])
{
}

argv is exactly what you want: an array of pointers to character strings. argc is just the number of arguments.

Upvotes: 2

Leigh
Leigh

Reputation: 12506

argc is the count or number of arguments that were handed to your program.

You'll need to parse the actual arguments from the double pointer argv. You do first need to list argv as input, though:

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

For an example, check out this page.

http://www.thegeekstuff.com/2013/01/c-argc-argv/

Upvotes: 2

Related Questions