Please tell us about how argc
and argv
arguments work in C / C ++ .
3 answers
The first type of int
stores the number of parameters to be transferred, one of the parameters is necessarily the name of the program (absolute path), then there may already be your parameters if this is your program.
argv
is usually an array of char
, but now it is _TCHAR
, with pointers, but _TCHAR
usually in the IDE as a char
, there are already the names of the parameters passed, one is the program, and the others are their order.
Example:
hello.exe -param1 -param2 argc = 3 argv[0] = hello.exe argv[1] = -param1 argv[2] = -param2
Here I am only with the forgotten path. If anything, the book is "Pakhomov Visual C \ C ++ 2010" , I read it there.
- Pointer dereference was not necessary. - alphard
- _TCHAR is a homegrown type, there is no such thing in the standard. - alexlz
- We know this about _TCHAR, but I pointed out about the ide, and the language and the ide in today's world are two different things though. Everyone sculpts as he wants, and the standard is more likely not for a coder, but for a compiler developer. - rojaster
- "
argv
is usually an array ofchar
" ym ... In all ages and times,argv
been an array of pointers . - AnT
argv
is an array of pointers to null-terminated lines containing command line parameters with which your program was called.If
argc
greater than zero, thenargv[0]
contains a pointer to the name of your program. How this name is presented is dependent on the implementation. If the program name is not provided,argv[0]
will point to an empty string (that is, cannot be a null pointer).If
argc
greater than one, thenargv[1]
...argv[argc - 1]
elements contain pointers to command line parameters .The size of this array is
argc + 1
(and notargc
, as is often mistakenly believed). This ensures thatargv[argc]
contains a null pointer. Thus, in order to find the end of theargv
array, you can either use theargc
value, or just browse through theargv
array before meeting the first null pointer.In the C language, it is allowed to modify both the elements of the
argv
array and the lines themselves, indicated by the elements of theargv
array (of course, within the limits of the initial length of the string). In C ++, this permission is not explicitly given.
- In addition to the pure theory of the C standard, it is worth mentioning how in practice you can work with these parameters, for example, argv is just an array of C lines that you can pass to
execv()
on Unix when creating a new process andargv[0]
for your needs can be set or so that arbitrary Unicode arguments can be taken on Windows, the portable code should be an analogue ofboost::nowide::args(argc, argv)
called (whichGetCommandLineW()
,CommandLineToArgvW()
calls to restore the arguments on Windows). .. - jfs - ... Or (to answer "how they work") an example of how arguments in memory can be placed - jfs
- @jfs: This is already a question for the author of the question: without clarification, this is a question about working with parameters on the receiving side. There is no difference between the “theory of standard” and “practice” in this case. No other "practice" is here and can not be. The link you provided is devoted to a completely different topic: transferring the environment through the advanced
main
parameters in POSIX. What does it have to do with this? In addition, stories about how such arguments can be stored in memory are about the same as stories about what numbersrand()
can generate. - AnT - Try to transfer to the console application via argv the name of the file with Unicode characters that cannot be represented on Windows in the current code page, and we will see if there is a difference between theory and practice. The question is not what the standard resolves, but how it works. Assuming that there is no world outside the standard and identifying it with rand () is poor: all abstractions are leaky, so see not only the abstraction itself, but also how it is implemented (at least one floor down) or at least as practical problems get around is helpful. And look carefully, the argv code is present - jfs
argc - 1
equals the number of arguments passed to the program on the command line.argv[1]
...argv[argc - 1]
are the arguments themselves.argv[0]
is the name of the program as entered on the command line.