I'm learning C++ and here I've written a little toy program that is supposed to list all the arguments passed to it through the command line.
#include <iostream>;
using namespace std;
int main (int argc, char *argv[]) {
for (int number = 1; number < argc; ++number) {
cout << argv[number] << endl;
}
return 0;
}
It is possible to use indexing instead and rewrite it in the following way:
#include <iostream>
using namespace std;
int main (int argc, char *argv[]) {
int i;
for (int j = 1; j < argc; ++j) {
i = 0;
while (argv[j][i]) {
cout << argv[j][i];
++i;
}
cout << endl;
}
return 0;
}
I personally like the first version more. It doesn't need one more variable and it doesn't need one more cycle, so making use of pointers here seems like a good idea: it helps improve readability and as far as I know performance.
Is this actually a good alternative to the second version that uses indexing? And if so, how could it be improved?