Question:
Please tell us about how the argc
and argv
arguments work in C / C ++ .
Answer:
-
argv
is an array of pointers to null-terminated strings containing the command line options your program was invoked with. -
If
argc
greater than zero, thenargv[0]
contains a pointer to the name of your program. How this name is presented is implementation dependent. If no program name is supplied,argv[0]
will point to an empty string (that is, it cannot be a null pointer). -
If
argc
greater than one, thenargv[1]
…argv[argc - 1]
contain pointers to command line parameters . -
The size of this array is
argc + 1
(notargc
, as is often mistakenly believed). This ensures thatargv[argc]
contains a null pointer. Thus, to find the end of theargv
array, you can either use theargc
value, or simply browse theargv
array until you encounter the first null pointer. -
In the C language, it is allowed to modify both the elements of the
argv
array and the strings themselves indicated by the elements of theargv
array (of course, within the original string length). C ++ does not explicitly grant this permission.