What is the type of command-line argument `argv` in C?

前端 未结 6 1011
广开言路
广开言路 2020-12-28 17:45

I\'m reading a section from C Primer Plus about command-line argument argv and I\'m having difficulty understanding this sentence.

It says that,

6条回答
  •  旧巷少年郎
    2020-12-28 17:57

    argv is of type char **. It is not an array. It is a pointer to pointer to char. Command line arguments are stored in the memory and the address of each of the memory location is stored in an array. This array is an array of pointers to char. argv points to first element of this array.

                      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. In most of the cases argv[0] represents the program name but if program name is not available from the host environment then argv[0][0] represents null character.

提交回复
热议问题