代码之家  ›  专栏  ›  技术社区  ›  Stephano

理解C++指针(指向指针时)

  •  11
  • Stephano  · 技术社区  · 16 年前

    我认为我很好地理解参考文献和指针。我想我知道的是:

    int i = 5; //i is a primitive type, the value is 5, i do not know the address.
    int *ptr;  //a pointer to an int. i have no way if knowing the value yet.
    ptr = &i;  //now i have an address for the value of i (called ptr)
    *ptr = 10; //Go to the value in ptr, use it to find a location and store 10 there
    

    请随时评论或更正这些陈述。

    现在我正在尝试跳转到指针数组。我不知道的是:

    char **char_ptrs = new char *[50];
    Node **node_ptrs = new Node *[50];
    

    我的理解是我有两个指针数组,一组指向字符的指针和一个指向节点的指针。所以如果我想设置这些值,我会这样做:

    char_ptrs[0] = new char[20];
    node_ptrs[0] = new Node;
    

    现在我有一个指针,在数组的0位置,在每个数组中。如果我感到困惑,请在此发表评论。

    那么,**操作员做什么?同样,在实例化旁边放一个*是做什么的([50])?(这到底叫什么,实例化?)

    5 回复  |  直到 16 年前
        1
  •  7
  •   Jon    16 年前

    一些评论:

    *ptr = 10; // Doesn't need to "go get" the value. Just overwrites it.
    

    也:

    char **char_ptrs = new char *[50];
    Node **node_ptrs = new Node *[50];
    

    我们更容易想到你有两个数组。但是,从技术上来说(就编译器而言),您拥有的是两个 指针 . 一个是指向(指向字符的指针)的指针,另一个是指向(指向节点的指针)。

    变量的声明很容易看出这一点,顺便说一下,从右到左最容易读取:

    char **char_ptrs
    

    从右向左阅读: char_ptrs 是指向 char

    将*放在指针旁边是正确调用的 取消引用 那个指针。由于数组在技术上不存在,因此数组上的运算符[]也是一个解引用操作: arr[i] 是另一种写作方式 *(arr + i) . 要正确理解这一点,您需要熟悉 pointer arithmetic .

    多个连续的*s:每个都取消引用它所操作表达式的结果。所以在写作时:

    char c = **char_ptrs;
    

    发生的是:

    查尔斯泰尔斯 是指向字符指针的指针。取消对它的引用一次(对于最右边的*)会得到它的值,它是一个指向char的指针。取消对该值的引用(对于最左边的*)会依次为您提供它自己的值,即char。最后, c 包含存储在内存中的char值,该值位于char指针指向的位置(换句话说,数组中的第一个指针)。

    相反,如果你写 **char_ptrs = 'a'; 然后您将更改该内存位置中的值。

        2
  •  6
  •   Ignacio Vazquez-Abrams    16 年前

    ** 只是 * 两次,一个指向指针的指针。

    当放在一个类型旁边时, * 向左绑定,而不是向右绑定。说 new char *[50] 实际上是 new char* [50] 并实例化一个50的数组 char* .

        3
  •  3
  •   Loki Astari    16 年前

    如果您发现*符号难以阅读,请使用typedef帮助您的代码易于阅读。

    typedef char*      CharPtr;
    typedef CharPtr*   CharPtrPtr;
    // Alternative to the line above
    // typedef char**     CharPtrPtr;
    
    // When you call new. You get a ptr to the type you are newing.
    // new int returns an intPtr. new char returns a charPtr
    CharPtrPtr char_ptrs = new CharPtr[50];
    
    // So new CharPtr returns a CharPtrPtr
    // In this case we return a pointer to contigious
    // chunk of memory large enough to hold 50 CharPtr objects.
    
        4
  •  2
  •   Nathan Osman    16 年前

    第一个代码块中的语句都是正确的。

    char **char_ptrs = new char *[50];
    

    …意味着你有一个50的数组 char * S.

    你对

    char_ptrs[0] = new char[20];
    node_ptrs[0] = new Node;
    

    也是正确的。

    ** 简单的意思是“指向指针的指针”。不是接线员。

    当你写作时

    new char *[50];
    

    …你说的是“为50分配存储空间” 烧焦* S’。

        5
  •  0
  •   Zoli    16 年前

    澄清第一部分:

    int i = 5; // i is a primitive type, the value is 5, the address is retrieved via &i.
    int *ptr;  // an unassigned pointer to an int
    ptr = &i;  // ptr now point to the address of variable i
    *ptr = 10; // access (dereference) the value through ptr and change it to 10 (same as i=10)
    

    没有**运算符,只有*运算符。正如其他人所说,**声明一个指向指针的指针。因为您声明的是指针数组,并且指针是用*运算符声明的,所以在为指针分配内存时,需要使用 new . 因此,您有:

    char **char_ptrs = new char *[50]; // allocates memory for 50 contiguous char* (pointers)
    Node **node_ptrs = new Node *[50]; // allocates memory for 50 contiguous Node* (pointers)
    

    指向指针的指针不必声明数组。您也可以使用另一个指针指向的常规指针,如:

    char i = 'p';
    char *myptr = &i;
    char **mysecondptr = &myptr;