I'm new to C and trying to figure out arrays and command line arguments. I have:
int main(int argc, int **argv) {
int vals[8];
for(int i = 0;i < 8;i = i + 1) {
vals[i] = atoi(argv[i]);
printf("%d", vals[i]);
}
}
I call it with ./file 1 2 3 4 5 6 7 8 and I would expect it to spit out 12345678, but instead, it spits out 01234567 which to me says that it's just printing the array positions. How do I get to actually print/access the value of vals[i], and/or make sure that the command line value is actually being properly assigned?
Thanks in advance.
./file 8 7 6 5 4 3 2 1.atoiissues 0 for your program name because it failed to convert to integer.atoi()is a bad idea and you should use one of thestrto___()functions./strto[a-z]+/will also return0but set errno in certain implementationsargv[0]is the name of the program (./file).atoi()will return0when given such a string.