What is ** in C++?

后端 未结 11 1280
野性不改
野性不改 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 14:53

    I just wanted to underscore some of the uses for a pointer to a pointer. Most of these are touched on by other posts, but I thought reiteration might help.

    • It allows a callee to modify a pointer owned by the caller. For example, one could pass a pointer to a pointer to the beginning of a string, and the callee could modify the pointed-to pointer to now point to a position within the string where a particular character occurs.

    • Because arrays degrade to pointers (and pointers can be treated as arrays), you will often see a pointer to a pointer if you have:

      • A pointer to an array. This is a generalization of the above case, since a "string" (a C-style string, anyway) is really just an array of chars.

      • An array of pointers. You might, for example, have an array of pointers to objects, allowing for polymorphism, or an array of pointers to select objects stored in another collection.

      • An array of arrays. Again, arrays degrade to pointers, so this is a specific case of the above. This is often used for so called "jagged" arrays (as opposed to rectangular).

提交回复
热议问题