int main(int argc, char** argv)
is the entry point of C and C++ programs, whereargc
andargv
is used to receive parameters passed to the program from the command line. Below I will explain the meaning of these two parameters in detail and give an example to help understand.
Parameter explanation
-
int argc
:argc
is the abbreviation of "argument count", which represents the number of command line parameters passed to the program.It is at least 1, because there is always at least one parameter on the command line: the name of the program.
-
char** argv
orchar *argv[]
:argv
is the abbreviation of "argument vector", which is a pointer to an array of character pointers.Each element
argv[i]
They are all pointers to C strings, which contain the numberi
Command line parameters.As usual,
argv[0]
It is the name of the program.argv
The array ends with a NULL pointer, but this NULL pointer is not usually included inargc
Inside the count.
Give an example
Suppose you have the following C++ program:
#include <iostream> int main(int argc, char** argv) { std::cout << "Program name: " << argv[0] << std::endl; for (int i = 1; i < argc; ++i) { std::cout << "Argument " << i << ": " << argv[i] << std::endl; } return 0; }
If you compile this program tomy_program
And run it from the command line as follows:
./my_program hello world 123
Program name: ./my_program Argument 1: hello Argument 2: world Argument 3: 123
In this example:
argc
The value of is 4 because there are four parameters: program name (./my_program
) and three user-provided parameters (hello
,world
,123
)。argv[0]
Point to string"./my_program"
。argv[1]
Point to string"hello"
。argv[2]
Point to string"world"
。argv[3]
Point to string"123"
。Note that there is no
argv[4]
, because the array index starts at 0 and in this example there are only four elements. Visitargv[4]
Create undefined behavior because it exceeds the boundaries of the array. In fact, according to the C and C++ standards,argv
Arrays are not guaranteed to end with a NULL pointer, although this may be the case on some systems. You should always rely onargc
to determine the number of parameters.
Examples based on OCCT:
This is a simple C++ program that is used to read STEP files and convert their contents into another format.
#include <STEPControl_Reader.hxx> int main(int argc, char** argv) { // Read from file. TopoDS_Shape shape; // if ( argc > 1 ) { STEPControl_Reader readerBase; (argv[1]); std::cout << () << "Base roots transferred" << std::endl; } else { std::cout << "Please, pass filename (STEP) as an argument." << std::endl; return 1; } return 0; }
The difference between char** argv and char* argv[]
- The pointer is an address.
- char *a: The value of a contains character data. Unreference to a (*a) gets a character, and does not dereference to a character string. printf("%c",*a) outputs a character or printf("%s", a) outputs a string;
- char **a: The value of a contains an address, which contains character-type data. Unreference to a (*a), get the address, and then unreference to the address (**a), get a character, printf("%c",**a) outputs a character printf("%s",*a) outputs a string;
- char *a[]: a is an array, the elements of the array are addresses (the address is actually a pointer), and the values contained in the address are character data. printf("%c",*a[i]) outputs a character, printf("%s", a[i])) outputs a string;
- char *a or char **a can change the value of a, char a*[] cannot change the value of a.
This is the article about the use of parameters of int main(int argc, char** argv) in C++. For more related contents of C++ int main(int argc, char** argv) please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!