The array subscript operation a[i]
is defined as *(a + i)
- given the address a
, offset i
elements (not bytes) from that address and dereference the result. Thus, given a pointer p
, *p
is equivalent to *(p + 0)
, which is equivalent to p[0]
.
The type of argv
is char **
; given that, all of the following are true:
Expression Type Value
---------- ---- -----
argv char ** Pointer to a sequence of strings
*argv char * Equivalent to argv[0]
**argv char Equivalent to argv[0][0]
argv[i] char * Pointer to a single string
*argv[i] char Same as argv[i][0]
argv[i][j] char j'th character of i'th string
&argv[i] char ** Address of the pointer to the i'th string
Since the type of argv[i][j]
is char
, *argv[i][j]
is not a valid expression.
Here's a bad visualization of the argv
sequence:
+---+ +---+ +---+
argv | | ---> argv[0] | | ---------------------------> argv[0][0] | |
+---+ +---+ +---+ +---+
argv[1] | | -------> argv[1][0] | | argv[0][1] | |
+---+ +---+ +---+
... argv[1][1] | | ...
+---+ +---+ +---+
argv[argc] | | ---||| ... argv[0][n-1] | |
+---+ +---+ +---+
argv[1][m-1] | |
+---+
This may help explain the results of different expressions.