Reputation: 479
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.
Upvotes: 0
Views: 10525
Reputation: 4366
Yes there is.
If you look at the main function's full prototype:
int main(int argc, char **argv, char **env)
argc: This is the argument counter, it contains the number of argument given by the user (Assumin the command is cd
, entering cd home
will 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 env
command).
As for an example of making an array of things: Two ways are possible:
First, a fixed-length array:
char tab[4]; // declares a variable "tab" which is an array of 4 chars
tab[0] = 'a'; // Sets the first char of tab to be the letter 'a'
Second, a variable-length array:
//You cannot do:
//int x = 4;
//char tab[x];
//Because the compiler cannot create arrays with variable sizes this way
//(If you want more info on this, look for heap and stack memory allocations
//You have to do:
int x = 4; //4 for example
char *tab;
tab = malloc(sizeof(*tab) * x); //or malloc(sizeof(char) * x); but I prefer *tab for
//many reasons, mainly because if you ever change the declaration from "char *tab"
//to "anything *tab", you won't have to peer through your code to change every malloc,
//secondly because you always write something = malloc(sizeof(*something) ...); so you
//have a good habit.
Using the array:
Any way you choose to declare it (fixed-size or variable-size), you always use an array the same way:
//Either you refer a specific piece:
tab[x] = y; //with x a number (or a variable containing a value inside your array boundaries; and y a value that can fit inside the type of tab[x] (or a variable of that type)
//Example:
int x = 42;
int tab[4]; // An array of 4 ints
tab[0] = 21; //direct value
tab[1] = x; //from a variable
tab[2] = tab[0]; //read from the array
tab[3] = tab[1] * tab[2]; //calculus...
//OR you can use the fact that array decays to pointers (and if you use a variable-size array, it's already a pointer anyway)
int y = 21;
int *varTab;
varTab = malloc(sizeof(*varTab) * 3); // An array of 3 ints
*varTab = y; //*varTab is equivalent to varTab[0]
varTab[1] = x; //Same as with int tab[4];
*(varTab + 2) = 3; //Equivalent to varTab[2];
//In fact the compiler interprets xxx[yyy] as *(xxx + yyy).
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.
Upvotes: 3
Reputation: 15256
int main(int argc, char *argv[])
That's what main should look like in your program. argc
is the number of arguments passed into the program. argv
is a list of strings which are the arguments passed into the program with argv[0] being the program name.
Upvotes: 0
Reputation: 13713
int main ( int argc, char *argv[] ) {
}
This is how you declare your main entry function.
argc
is the number of arguments input by the user INCLUDING the program name which is the first string in argv
(argv[0]
= program name).
and since argv is an already array of strings I suggest you use as this is what it is for. (Just don't forget to skip index 0)
Upvotes: 0