Please tell us about how argc and argv arguments work in C / C ++ .

  • The answer to this question is in any textbook on C. Why ask about this here? - IAZ
  • But how can you implement a function that takes an unlimited number of arguments: stdargs.h - outoftime

3 answers 3

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 of char " 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, then argv[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, then argv[1] ... argv[argc - 1] elements contain pointers to command line parameters .

  • The size of this array is argc + 1 (and not argc , as is often mistakenly believed). This ensures that argv[argc] contains a null pointer. Thus, in order to find the end of the argv array, you can either use the argc value, or just browse through the argv 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 the argv 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 and argv[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 of boost::nowide::args(argc, argv) called (which GetCommandLineW() , 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 numbers rand() 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.