PASSING COMMAND LINE ARGUMENTS

Write your question here.
I passed Hello World as the command argument using the Debugger. My output does not come out as expected. I get a page of garbage and not argv-0 Hello, and World on separate lines. What's wrong.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// argv-0
// Print command-line arguments, one per line
// Demonstrates use of argv

#include <cstdio>
#include <iostream>
#include <string>
using namespace std;


int main(int argc, string argv[])
{
	// print arguments
	printf("\n");
	for (int i = 0; i < argc; i++)
	{
		cout << argv[i] << "\n";
	}
	printf("\n");
}
1
2
// int main(int argc, string argv[])
int main( int argc, char* argv[] )
Thank you. It works. However, rather that just give me the answer, please explain why string does not work and why char* works.

> please explain why string does not work and why char* works.

The main() function is very special.

The C++ standard specifies that all (hosted) implementations must support these two forms of main():
1
2
int main() { /* ... */ }
int main( int argc, char* argv[] ) { /* ... */  }

See: http://en.cppreference.com/w/cpp/language/main_function
Thank you very much.
Topic archived. No new replies allowed.