Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Pointers-to-pointers (hence pointers-to-arrays by proxy) are really useful. If you have a function/method and it takes a pointer-to-value argument you can change the value inside your function and that value stays in scope after you leave the function - pass-by-reference of course. However you can't change the address your pointer points at - e.g. make the pointer you passed into a NULL pointer, or point it at a different value somewhere else in memory. If you use a pointer-to-pointer-to-value then you can change the value of the 'middle' pointer inside your function. I think the MySQL C-connector library is an example of where this is used.</p> <p>In your example you could pass <code>ptrToAnOneDimArray</code> into a function and make *ptrToAnOneDimArray be a NULL pointer or a pointer to some other data rather than <code>intArrayOne</code> - as <code>intArrayOne</code> is a fixed size by the compiler (on the stack) then you could dynamically update *ptrToAnOneDimArray from the stack to be an array malloc()'d on the heap.</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #define SIZE 3 void display(char* data) { int i = 0; for(i=0 ; i&lt;SIZE ; i++) { printf("%c ", data[i]); } } void changeMyArgument(char** pointerToPointer) { *pointerToPointer = (char*) malloc(SIZE * sizeof(char)); /* now we use array notation for a change */ (*pointerToPointer)[0] = 'X'; (*pointerToPointer)[1] = 'Y'; (*pointerToPointer)[2] = 'Z'; } int main() { /* intArrayOne is implicitly char* */ char intArrayOne[SIZE] = {'A', 'B', 'C'}; char* arraysArePointers = intArrayOne; /* ptrToAnOneDimArray is implicitly char** */ char** ptrToAnOneDimArray; ptrToAnOneDimArray = &amp;arraysArePointers; display(*ptrToAnOneDimArray); changeMyArgument(ptrToAnOneDimArray); display(*ptrToAnOneDimArray); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload