Home  >  Q&A  >  body text

c++ - Problem with pointers in c language

*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

给我你的怀抱给我你的怀抱2690 days ago992

reply all(7)I'll reply

  • 曾经蜡笔没有小新

    曾经蜡笔没有小新2017-06-30 09:59:22

    argv[1] is equivalent to *(argv+1)

    reply
    0
  • 我想大声告诉你

    我想大声告诉你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.

    reply
    0
  • PHP中文网

    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.

    reply
    0
  • 怪我咯

    怪我咯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

    reply
    0
  • 巴扎黑

    巴扎黑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.

    reply
    0
  • 伊谢尔伦

    伊谢尔伦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.

    reply
    0
  • 为情所困

    为情所困2017-06-30 09:59:22

    In C language, an array is originally an address

    reply
    0
  • Cancelreply