Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You <a href="http://c-faq.com/aryptr/arraylval.html" rel="noreferrer">can't assign anything to an array variable in C</a>. It's not a 'modifiable lvalue'. From the spec, <strong>§6.3.2.1 Lvalues, arrays, and function designators</strong>:</p> <blockquote> <p>An <em>lvalue</em> is an expression with an object type or an incomplete type other than <strong><code>void</code></strong>; if an lvalue does not designate an object when it is evaluated, the behavior is undefined. When an object is said to have a particular type, the type is specified by the lvalue used to designate the object. A <em>modifiable lvalue</em> is an lvalue that does not have array type, does not have an incomplete type, does not have a const-qualified type, and if it is a structure or union, does not have any member (including, recursively, any member or element of all contained aggregates or unions) with a const-qualified type.</p> </blockquote> <p>The error message you're getting is a bit confusing because the array on the right hand side of the expression decays into a pointer before the assignment. What you have is semantically equivalent to:</p> <pre><code>message1 = &amp;message2[0]; </code></pre> <p>Which gives the right side type <code>char *</code>, but since you still can't assign anything to <code>message1</code> (it's an array, type <code>char[100]</code>), you're getting the compiler error that you see. You can solve your problem by using <a href="http://linux.die.net/man/3/memcpy" rel="noreferrer"><code>memcpy(3)</code></a>:</p> <pre><code>memcpy(message1, message2, sizeof message2); </code></pre> <p>If you really have your heart set on using <code>=</code> for some reason, you could use use arrays inside structures... that's not really a recommended way to go, though.</p>
 

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