我正在阅读 C Primer Plus 中关于命令行参数 argv
的部分,但我很难理解这句话。
它说,
The program stores the command line strings in memory and stores the address of each string in an array of pointers. The address of this array is stored in the second argument. By convention, this pointer to pointers is called
argv
, for argument values .
这是否意味着命令行字符串作为指向 char
数组的指针数组存储在内存中?
最佳答案
argv
是 char **
类型。 它不是数组。它是指向 char
的指针。命令行参数存储在内存中,每个内存位置的地址存储在一个数组中。该数组是指向 char
的指针数组。 argv
指向这个数组的第一个元素。
Some array +-------+ +------+------+-------------+------+ argv ----------> | | | | | | | | 0x100 +------> | | | . . . . . . | | Program Name1 0x900 | | | | | | | | | +------+------+-------------+------+ +-------+ 0x100 0x101 | | +------+------+-------------+------+ | 0x205 | | | | | | 0x904 | +------> | | | . . . . . . | | Arg1 | | . | | | | | +-------+ +------+------+-------------+------+ | . | . 0x205 0x206 | . | | . | . | . | +-------+ . +------+------+-------------+------+ | | | | | | | | 0x501 +------> | | | . . . . . . | | Argargc-1 | | | | | | | +-------+ +------+------+-------------+------+ | | 0x501 0x502 | NULL | | | +-------+ 0xXXX Represents memory address
<子>1。在大多数情况下,argv[0]
表示程序名称,但如果程序名称在主机环境中不可用,则 argv[0][0]
表示空字符。
关于c - C 中命令行参数 `argv` 的类型是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39095850/