*argv[]
argv is the address, *argv[] is the variable pointed to by the address.
Since argv is an address, why can argv[1] be called like this
我想大声告诉你2017-06-30 09:59:22
You must be asking about int main(int argc, char *argv[])
. In fact, there is no problem at all if you want to write like this int main(int argc, char **argv)
.
If you define a char *s = '2333'
, what does this s
refer to? The first address of the string. In the same way, what does *argv[1]
store? The first address of the first string. You can simply understand it this way. If you want to go deeper, you can read the book.
char *s = "23333";
char *z = "34444";
char *argv[] = {s, z};
int a = 0;
int b = 1;
int c[] = {a, b};
It’s interesting~ In fact, sometimes if you want to understand, it’s just a matter of changing the way you write it.
PHP中文网2017-06-30 09:59:22
The original poster wants to say
int main(int argc, char *argv[])
*argv[] in
, right?
char *argv[]
is an array. The elements of the array are char *
. Each char *
is used to point to the first address of a string. So argv[1] points to the first address of the second string (we assume that the first string is argv[0]). Therefore, argv[1]
is also an address, which is the first address of a string.
Arrays and pointers can only be considered the same when used as function parameters, so char *argv[]
here can also be written as char **argv
. I don’t know if this is easier to understand.
怪我咯2017-06-30 09:59:22
The type when passing parameters is *argv[], which is equivalent to **argv, which is a pointer to a pointer
巴扎黑2017-06-30 09:59:22
Here*argv[]
defines a pointer array, with n array elements of pointer type (argv[0],argv[1],...,argv[n]
)
(argv[1 ]
is the second element of the pointer array, which is still a pointer. *(argv+1)
is the value pointed to by the second element of the pointer array, which is a value )
argv[1]
is the address. How to call it needs to be analyzed based on the specific context code.
Generally, argv[1]
is placed in another pointer int *p = (int*)argv[1];
, and then *p
gets the value of *argv[1]
below.
伊谢尔伦2017-06-30 09:59:22
The name of the array is actually an address, so there is nothing wrong with using it this way.