If you define your main
with the signature as
int main(int argc, char *argv[]);
then, here argv
is an array of pointers to strings passed as command line arguments. Quoting the C99 standard section 5.1.2.2.1 -
The parameters argc and argv and the strings pointed to by the argv
array shall be modifiable by the program, and retain their last-stored
values between program startup and program termination.
Therefore, you can either directly modify the strings pointed to by elements of argv
, or you can copy those strings and then process them.
#include <stdlib.h>
int main(int argc, char *argv[]) {
char *strlist[argc];
int i = 0;
while(i < argc) {
strlist[i] = malloc(1 + strlen(argv[i]));
if(strlist[i] == NULL) {
printf("not enough memory to allocate\n");
// handle it
}
strcpy(strlist[i], argv[i]);
i++;
}
// process strlist
// after you are done with it, free it
for(i = 0; i < argc; i++) {
free(strlist[i]);
strlist[i] = NULL;
}
return 0;
}