I"m a beginner C programmer, yesterday I learned the use of C structs and the possible application of these ones about the resolution of specific problems. However when I was experimenting with my C IDE (Codeblocks 16.01) in order to learn this aspect of C programming, I"ve encountered a strange issue. The code is the following:

#include #define N 30typedef struct char name; char surname; int age; data;int main() data s1; s1.name="Paolo"; s1.surname = "Rossi"; s1.age = 19; getchar(); return 0;During the compilation, the compiler (GCC 4.9.3-1 under Windows) reported me an error that says

"error: assignment to expression with array type error"

on instruction

s1.name="Paolo" s1.surname="Rossi" while if I do

data s1 = "Paolo", "Rossi", 19;it works. What am I doing wrong?

c arrays string struct initialization
edited Jan 4 "19 at 6:48

Sourav Ghosh
129k1616 gold badges172172 silver badges243243 bronze badges
asked May 14 "16 at 10:02

59511 gold badge44 silver badges66 bronze badges
Add a comment |

3 Answers 3

Active Oldest Votes
You are facing issue in

s1.name="Paolo";because, in the LHS, you"re using an array type, which is not assignable.

You are watching: Error: assignment to expression with array type

To elaborate, from C11, chapter §6.5.16

assignment operator shall have a modifiable lvalue as its left operand.

and, regarding the modifiable lvalue, from chapter §

A modifiable lvalue is an lvalue that does not have array type, <...>

You need to use strcpy() to copy into the array.

See more: International Association Of Assistance Dog Partners, Assistance Dogs International

That said, data s1 = "Paolo", "Rossi", 19; works fine, because this is not a direct assignment involving assignment operator. There we"re using a brace-enclosed initializer list to provide the initial values of the object. That follows the law of initialization, as mentioned in chapter §6.7.9

Each brace-enclosed initializer list has an associated current object. When no designations are present, subobjects of the current object are initialized in order according to the type of the current object: array elements in increasing subscript order, structure members in declaration order, and the first named member of a union.<....>