F1e3ab214a976a39cfd713bc93deb10f

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?

int 
main(int argc, char **argv) {
  while (*++argv) printf("%s\n", *argv); 
  return 0;
}

Refactorings

No refactoring yet !

729442eea8d8548842a6e0947e333c7b

Chris Jester-Young

April 30, 2009, April 30, 2009 21:40, permalink

2 ratings. Login to rate!

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;
}
A8d3f35baafdaea851914b17dae9e1fc

Adam

May 7, 2009, May 07, 2009 04:34, permalink

1 rating. Login to rate!

I prefer a bog standard for loop. But if you want something lighter:

main(int c,int **a){*++a&&main(puts(*a),a);}
F1e3ab214a976a39cfd713bc93deb10f

Tj Holowaychuk

May 9, 2009, May 09, 2009 06:57, permalink

No rating. Login to rate!

haha not necessarily that light, but something sensible, for loops i find are just overkill in some situations

D41d8cd98f00b204e9800998ecf8427e

Oddity

October 28, 2009, October 28, 2009 09:58, permalink

No rating. Login to rate!

> 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.

Your refactoring





Format Copy from initial code

or Cancel