Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If by "shallow copy", you mean that after assignment of a <code>struct</code> containing an array, the array would point to the original <code>struct</code>'s data, then: it can't. Each element of the array has to be copied over to the new <code>struct</code>. "Shallow copy" comes into the picture if your struct has pointers. If it doesn't, you <em>can't</em> do a shallow copy.</p> <p>When you assign a <code>struct</code> containing an array to some value, it cannot do a shallow copy, since that would mean assigning to an array, which is illegal. So the only copy you get is a deep copy.</p> <p>Consider:</p> <pre><code>#include &lt;stdio.h&gt; struct data { char message[6]; }; int main(void) { struct data d1 = { "Hello" }; struct data d2 = d1; /* struct assignment, (almost) equivalent to memcpy(&amp;d2, &amp;d1, sizeof d2) */ /* Note that it's illegal to say d2.message = d1.message */ d2.message[0] = 'h'; printf("%s\n", d1.message); printf("%s\n", d2.message); return 0; } </code></pre> <p>The above will print:</p> <pre><code>Hello hello </code></pre> <p>If, on the other hand, your <code>struct</code> had a pointer, <code>struct</code> assignment will only copy pointers, which is "shallow copy":</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;string.h&gt; struct data { char *message; }; int main(void) { struct data d1, d2; char *str = malloc(6); if (str == NULL) { return 1; } strcpy(str, "Hello"); d1.message = str; d2 = d1; d2.message[0] = 'h'; printf("%s\n", d1.message); printf("%s\n", d2.message); free(str); return 0; } </code></pre> <p>The above will print:</p> <pre><code>hello hello </code></pre> <p>In general, given <code>struct T d1, d2;</code>, <code>d2 = d1;</code> is equivalent to <code>memcpy(&amp;d2, &amp;d1, sizeof d2);</code>, but if the struct has padding, that may or may not be copied.</p> <p><em>Edit</em>: In C, you <a href="https://stackoverflow.com/questions/2035066/type-of-an-array/2035255#2035255">can't assign to arrays</a>. Given:</p> <pre><code>int data[10] = { 0 }; int data_copy[10]; data_copy = data; </code></pre> <p>is illegal. So, as I said above, if you have an array in a <code>struct</code>, assigning to the struct <em>has to</em> copy the data element-wise in the array. You don't get shallow copy in this case: it doesn't make any sense to apply the term "shallow copy" to a case like this.</p>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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