<p>Under what circumstances might you want to use multiple indirection (that is, a chain of pointers as in <code>Foo **</code>) in C++?</p>

[{'answer_id': 71143, 'author': 'aku', 'author_id': 1196, 'author_profile': 'https://Stackoverflow.com/users/1196', 'pm_score': 3, 'selected': False, 'text': '<p>IMO most common usage is to pass reference to pointer variable</p>\n\n<pre><code>void test(int ** var)\n{\n ...\n}\n\nint *foo = ...\ntest(&amp;foo);\n</code></pre>\n\n<p>You can create multidimensional jagged array using double pointers: </p>\n\n<pre><code>int ** array = new *int[2];\narray[0] = new int[2];\narray[1] = new int[3];\n</code></pre>\n'}, {'answer_id': 71154, 'author': 'Carl Seleborg', 'author_id': 2095, 'author_profile': 'https://Stackoverflow.com/users/2095', 'pm_score': 2, 'selected': False, 'text': '<p>If you pass a pointer in as output parameter, you might want to pass it as <code>Foo**</code> and set its value as <code>*ppFoo = pSomeOtherFoo</code>.</p>\n\n<p>And from the algorithms-and-data-structures department, you can use that double indirection to update pointers, which can be faster than for instance swapping actual objects.</p>\n'}, {'answer_id': 71160, 'author': 'dudico', 'author_id': 11089, 'author_profile': 'https://Stackoverflow.com/users/11089', 'pm_score': 1, 'selected': False, 'text': "<p>A simple example would be using <code>int** foo_mat</code> as a 2d array of integers.\nOr you may also use pointers to pointers - lets say that you have a pointer <code>void* foo</code> and you have 2 different objects that have a reference to it with the following members: <code>void** foo_pointer1</code> and <code>void** foo_pointer2</code>, by having a pointer to a pointer you can actually check whether <code>*foo_pointer1 == NULL</code> which indicates that foo is NULL. You wouldn't be able to check whether foo is NULL if foo_pointer1 was a regular pointer.\nI hope that my explanation wasn't too messy :)</p>\n"}, {'answer_id': 71164, 'author': 'Skizz', 'author_id': 1898, 'author_profile': 'https://Stackoverflow.com/users/1898', 'pm_score': 1, 'selected': False, 'text': '<p>Usually when you pass a pointer to a function as a return value:</p>\n\n<pre><code>ErrorCode AllocateObject (void **object);\n</code></pre>\n\n<p>where the function returns a success/failure error code and fills in the object parameter with a pointer to the new object:</p>\n\n<pre><code>*object = new Object;\n</code></pre>\n\n<p>This is used a lot in COM programming in Win32.</p>\n\n<p>This is more of a C thing to do, in C++ you can often wrap this type of system into a class to make the code more readable.</p>\n'}, {'answer_id': 71175, 'author': '0124816', 'author_id': 11521, 'author_profile': 'https://Stackoverflow.com/users/11521', 'pm_score': 1, 'selected': False, 'text': '<p>Carl: Your example should be:</p>\n\n<pre><code>*p = x;\n</code></pre>\n\n<p>(You have two stars.) :-)</p>\n'}, {'answer_id': 71250, 'author': 'John B', 'author_id': 11773, 'author_profile': 'https://Stackoverflow.com/users/11773', 'pm_score': 3, 'selected': False, 'text': "<p>One common scenario is where you need to pass a <strong>null</strong> pointer to a function, and have it initialized within that function, and used outside the function. Without multplie indirection, the calling function would never have access to the initialized object.</p>\n\n<p>Consider the following function:</p>\n\n<pre><code>initialize(foo* my_foo)\n{\n    my_foo = new Foo();\n}\n</code></pre>\n\n<p>Any function that calls 'initialize(foo*)' will not have access to the initialized instance of <strong>Foo</strong>, beacuse the pointer that's passed to this function is a copy. (The pointer is just an integer after all, and integers are passed by value.)</p>\n\n<p>However, if the function was defined like this:</p>\n\n<pre><code>initialize(foo** my_foo)\n{\n    *my_foo = new Foo();\n}\n</code></pre>\n\n<p>...and it was called like this...</p>\n\n<pre><code>Foo* my_foo;\n\ninitialize(&amp;my_foo);\n</code></pre>\n\n<p>...then the caller would have access to the initialized instance, via 'my_foo' - because it's the <em>address</em> of the pointer that was passed to 'initialize'. </p>\n\n<p>Of course, in my simplified example, the 'initialize' function could simply return the newly created instance via the return keyword, but that does not always suit - maybe the function needs to return something else.</p>\n"}, {'answer_id': 71298, 'author': 'Jorge Ferreira', 'author_id': 6508, 'author_profile': 'https://Stackoverflow.com/users/6508', 'pm_score': 5, 'selected': True, 'text': '<p>Most common usage as @aku pointed out is to allow a change to a pointer parameter to be visible after the function returns.</p>\n\n<pre><code>#include &lt;iostream&gt;\n\nusing namespace std;\n\nstruct Foo {\n    int a;\n};\n\nvoid CreateFoo(Foo** p) {\n    *p = new Foo();\n    (*p)-&gt;a = 12;\n}\n\nint main(int argc, char* argv[])\n{\n    Foo* p = NULL;\n    CreateFoo(&amp;p);\n    cout &lt;&lt; p-&gt;a &lt;&lt; endl;\n    delete p;\n    return 0;\n}\n</code></pre>\n\n<p>This will print</p>\n\n<pre><code>12\n</code></pre>\n\n<p>But there are several other useful usages as in the following example to iterate an array of strings and print them to the standard output.</p>\n\n<pre><code>#include &lt;iostream&gt;\n\nusing namespace std;\n\nint main(int argc, char* argv[])\n{\n    const char* words[] = { "first", "second", NULL };\n    for (const char** p = words; *p != NULL; ++p) {\n        cout &lt;&lt; *p &lt;&lt; endl;\n    }\n\n    return 0;\n}\n</code></pre>\n'}, {'answer_id': 73382, 'author': 'mlbrock', 'author_id': 9966, 'author_profile': 'https://Stackoverflow.com/users/9966', 'pm_score': 1, 'selected': False, 'text': '<p>In C, the idiom is absolutely required. Consider the problem in which you want a function to add a string (pure C, so a char *) to an array of pointers to char *. The function prototype requires three levels of indirection:</p>\n\n<pre><code>int AddStringToList(unsigned int *count_ptr, char ***list_ptr, const char *string_to_add);\n</code></pre>\n\n<p>We call it as follows:</p>\n\n<pre><code>unsigned int   the_count = 0;\nchar         **the_list  = NULL;\n\nAddStringToList(&amp;the_count, &amp;the_list, "The string I\'m adding");\n</code></pre>\n\n<p>In C++ we have the option of using references instead, which would yield a different signature. But we still need the two levels of indirection you asked about in your original question:</p>\n\n<pre><code>int AddStringToList(unsigned int &amp;count_ptr, char **&amp;list_ptr, const char *string_to_add);\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/71108', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/11575/']