What is ** in C++?

后端 未结 11 1260
野性不改
野性不改 2020-12-04 14:24

I\'ve seen some code, as well as some errors generated from my compiler that have a \'**\' token before the variable (eg **variablename unreferenced-- or someth

相关标签:
11条回答
  • 2020-12-04 15:02

    It's a double dereference.

    int i = 3;
    int* ptr_to_i = &i;
    int** ptr_to_ptr_to_i = &ptr_to_i;
    
    std::cout << **ptr_to_ptr_to_i << std::endl;
    

    Prints 3.

    0 讨论(0)
  • 2020-12-04 15:06

    You may recognize the signature for main():

    int main(int argc, char* argv[])
    

    The following is equivalent:

    int main(int argc, char** argv)
    

    In this case, argv is a pointer to an array of char*.

    In C, the index operator [] is just another way of performing pointer arithmetic. For example,

    foo[i]
    

    produces the same code as

    *(foo + i)
    
    0 讨论(0)
  • 2020-12-04 15:09
    • int **var declares a pointer to a pointer
    • **var references the content of a pointer, which in itself points to a pointer
    0 讨论(0)
  • 2020-12-04 15:14

    It's not a ** token. It's simply a * token followed by another * token. In your case, you have a pointer to a pointer, and it's being dereferenced twice to get whatever's really being pointed to.

    0 讨论(0)
  • 2020-12-04 15:14

    See http://www.c2.com/cgi/wiki?ThreeStarProgrammer

    0 讨论(0)
提交回复
热议问题