Note that there are some explanatory texts on larger screens.

plurals
  1. POIn assignment operator function, is array being memcpy implicitly
    primarykey
    data
    text
    <p>OK. We know the following code cannot be compiled.</p> <pre><code>char source[1024]; char dest[1024]; // Fail. Use memcpy(dest, source, sizeof(source)); instead. dest = source; </code></pre> <p>But, the following code can be compiled and behave correctly.</p> <pre><code>class A { char data[1024]; }; A source; B dest; dest = source; </code></pre> <p>I was wondering, in operator assignment function, is array will be memcpy implicitly?</p> <p>The following are the complete test code.</p> <hr> <pre><code>#include &lt;cstdio&gt; #include &lt;memory&gt; class A { public: char data[1024]; }; int main() { { A source; A dest; // Initialization char *data = "hello world"; memcpy (source.data, data, strlen(data) + 1); printf ("source.data = %s\n", source.data); printf ("address source.data = %x\n", source.data); // Works! Does this in the operator assignment function, memcpy is // being performed implicitly on array. dest = source; printf ("dest.data = %s\n", dest.data); printf ("address dest.data = %x\n", dest.data); } { char source[1024]; char dest[1024]; // Initialization char *data = "hello world"; memcpy (source, data, strlen(data) + 1); printf ("source = %s\n", source); printf ("address source = %x\n", source); // '=' : left operand must be l-value // dest = source; // Works with memcpy. memcpy(dest, source, sizeof(source)); printf ("dest = %s\n", dest); printf ("address dest = %x\n", dest); } getchar(); } //RESULT : //source.data = hello world //address source.data = 12fb60 //dest.data = hello world //address dest.data = 12f758 //source = hello world //address source = 12f344 //dest = hello world //address dest = 12ef3c </code></pre>
    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.
 

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