The relationship of an array, pointers and command line arguments in C++ programming

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows 2003 Server Standard Edition

Header file: Standard

Additional project setting: Set project to be compiled as C++

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)

Other info: This program must be run at command prompt

To do: Printing the command line arguments read from the standard input demonstrating the array, pointers and command line arguments in C++ programming

To show: The relationship of an array, pointers and the command line arguments in C/C++ programming

 

/* program to print arguments from command line, run this program at the command prompt */

#include <iostream>

using namespace std;

 

/* or int main(int argc, char *argv[ ]) - ANSI

or int wmain(int argc, WCHAR *argv[ ]) - Multibyte/Unicode

or int _tmain(int argc, TCHAR *argv[ ]) - Both: ANSI and Multibyte/Unicode

*/

int main(int argc, char **argv)

{

int i;

 

cout<<"argc = "<<argc<<endl;

for (i=0; i<argc; ++i)

cout<<"argv["<<i<<"]: "<<argv[i]<<endl;

 

return 0;

}

 

Output examples:

 

C:\amad\MyProgram\Debug>MyProgram

argc = 1

argv[0]: MyProgram

 

C:\amad\MyProgram\Debug>MyProgram argument1 argument2 argument3 argument4 argumentn

argc = 6

argv[0]: MyProgram

argv[1]: argument1

argv[2]: argument2

argv[3]: argument3

argv[4]: argument4

argv[5]: argumentn

 

C:\amad\MyProgram\Debug>

 

 

C and C++ Programming Resources | C & C++ Code Example Index