There’s no way to know how many arguments there are; the user can provide a list of indeterminate length.
I’m very bad with C. How can I read arguments out of the command-line array and into a new array of strings?
Frankly I don’t even know how to make an array of separate strings, if I’m going to be honest. An example would be super-helpful.
Yes there is.
If you look at the main function’s full prototype:
argc: This is the argument counter, it contains the number of argument given by the user (Assumin the command is
cd, enteringcd homewill give argc = 2 because the command name is always argument 0)argv: This is the arguments values, it is an array of size argc of
char*pointing to the arguments themselves.env: This is a table (as argv) containing the environment when the program is called (through a shell for example, it’s given with
envcommand).As for an example of making an array of things: Two ways are possible:
First, a fixed-length array:
Second, a variable-length array:
Using the array:
Any way you choose to declare it (fixed-size or variable-size), you always use an array the same way:
Star-ing a variable is called dereferencing. If you don’t know how this works I highly suggest you take a look.
I hope this is explained well-enough. If you still have questions please comment and I’ll edit this answer.