int
main(int argc, char **argv) {
while (*++argv) printf("%s\n", *argv);
return 0;
}
Refactorings
No refactoring yet !
Chris Jester-Young
April 30, 2009, April 30, 2009 21:40, permalink
Your method is mostly good, but, it will fail if your argc is 0 (i.e., argv[0] is null, and other elements after it are invalid). On some operating systems (e.g., Linux), if you call execve with an empty argument array (without even the program name as the 0th argument), this will in fact cause the above-mentioned condition.
Mind you, I once sent a patch to Boost.Program_options because it didn't catch the argc == 0 case either, so I guess it's not a corner case that many people think about. :-)
int
main(int argc, char **argv)
{
while (--argc >= 0)
printf("%s\n", *++argv);
return 0;
}
Adam
May 7, 2009, May 07, 2009 04:34, permalink
I prefer a bog standard for loop. But if you want something lighter:
main(int c,int **a){*++a&&main(puts(*a),a);}
Tj Holowaychuk
May 9, 2009, May 09, 2009 06:57, permalink
haha not necessarily that light, but something sensible, for loops i find are just overkill in some situations
Oddity
October 28, 2009, October 28, 2009 09:58, permalink
> it will fail if your argc is 0 (i.e., argv[0] is null
It is illegal for argv[0] to be null according to the C standard. The environment MUST fill argv[0] with something no matter what.
This is the cleanest way I could come up with, I am not a huge fan of for loops when you dont really need them but thats an option too of course. Is there a lighter way?