Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>I have a char ** array that has 3 pointers </p> </blockquote> <p>No, you don't. You have an array with three values. The values happen to be pointers. An array doesn't <em>have</em> pointers.</p> <p>Suppose you allocate memory for this array (of pointers to char):</p> <pre><code>char **array = malloc( 3 * sizeof(char*)); // 3 values in the array </code></pre> <p>and then you allocate memory for each string:</p> <pre><code>array[0] = malloc(4 * sizeof(char) ); // 3 + 1 null terminator array[1] = malloc(4 * sizeof(char) ); array[2] = 0; </code></pre> <p>finally you set the values for the strings themselves:</p> <pre><code>strcpy( array[0], "abc" ); strcpy( array[1], "def" ); </code></pre> <p>Now you want to make a copy. A <em>deep</em> copy actually, because you not only allocate memory for a new array (of pointers to char) but you allocate memory for new strings as well. You do exactly the same allocation:</p> <pre><code>char **array2 = malloc( 3 * sizeof(char*)); array2[0] = malloc(4 * sizeof(char) ); array2[1] = malloc(4 * sizeof(char) ); array2[2] = 0; </code></pre> <p>Now you copy the actual strings:</p> <pre><code>strcpy( array2[0], array[0] ); strcpy( array2[1], array[1] ); </code></pre> <p>If your code differs from the one above, edit your question with more details.</p> <p>The tricky part is making array[0] hold the address which is in array2[0] (same for array[1] and array2[1]). The problem comes from the fact that memory you allocate via <code>malloc</code> needs to be freed via <code>free</code>.</p> <p>So you <strong>can't</strong> just do</p> <pre><code>array[0] = array2[0]; </code></pre> <p>Because you've lost all references to the first string you allocated. That memory is <em>leaked</em>.</p> <p>Instead do this:</p> <pre><code>free(array[0]); array[0] = array2[0]; </code></pre> <p>This frees the memory used for the first string and copies the address of the copy. </p> <p>Now you have the same pointer (value) in two variables. This is also tricky because (when you no longer need it) you still need to free the memory allocated for the copy:</p> <p><code>free(array2[0])</code></p> <p>The <em>tricky</em> part is that <code>array[0]</code> now holds a pointer to freed memory, which can't be used. Using it will lead to bugs.</p> <p>It would probably be a good idea not to have multiple variables (in the same scope) that hold the same pointer to allocated memory. That may make it difficult to reason about the validity of the pointer.</p>
    singulars
    1. This table or related slice is empty.
    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