Argc Vs Argv
You will see argc
and argv
in
int main(int argc, char *argv[]) {
//...
}
argc
: argument countargv
argument array
For example, if we're calling
./main first second third
In this case, argc
is 4
.
argv
is an array with ["./main", "first", "second", "third"]
#include <stdio.h>
#include <stdlib.h>
char* parseArgv(int argc, char* argv[]);
int main(int argc, char* argv[]) {
printf("argc %d \n", argc);
printf("argv %s \n", parseArgv(argc, argv));
}
char* parseArgv(int argc, char* argv[]) {
char* result = (char*) malloc(100 * sizeof(char));
int pointer = 0;
for (int i = 0; i < argc; i++) {
for (int j = 0; argv[i][j] != '\0'; j++) {
result[pointer++] = argv[i][j];
}
result[pointer++] = ' ';
}
result[pointer] = '\0';
return result;
}