0

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?

1
  • what you are trying is totally unneeded. the arguments are already in an array... starting with your own executable argv[0]. Commented Nov 6, 2013 at 17:02

2 Answers 2

2

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/

Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.