I"m a beginner C programmer, yesterday I learned the use of C structs and the feasible application of these ones around the resolution of particular difficulties. However before once I was experimenting with my C IDE (Codeblocks 16.01) in order to learn this aspect of C programming, I"ve encountered a strange concern. 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(); rerevolve 0;During the compilation, the compiler (GCC 4.9.3-1 under Windows) reported me an error that says

"error: assignment to expression via variety kind error"

on instruction

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

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


c arrays string struct initialization
Share
Improve this question
Follow
edited Jan 4 "19 at 6:48
*

Sourav Ghosh
128k1616 gold badges170170 silver badges239239 bronze badges
asked May 14 "16 at 10:02
*

p4nzer96p4nzer96
57511 gold badge44 silver badges66 bronze badges
3
Add a comment |

3 Answers 3


Active Oldest Votes
68
You are facing concern in

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

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

To intricate, from C11, chapter §6.5.16

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

and also, regarding the modifiable lvalue, from chapter §6.3.2.1

A modifiable lvalue is an lworth that does not have actually range form, <...>

You should usage strcpy() to copy into the range.

See more: Helms Deep: Server Is Enforcing Consistency For This File Consistency

That sassist, information s1 = "Paolo", "Rossi", 19; works fine, bereason this is not a direct assignment entailing assignment operator. Tright here we"re using a brace-enclosed initializer list to provide the initial worths of the object. That adheres to the legislation of initialization, as pointed out in chapter §6.7.9

Each brace-enclosed initializer list has actually an linked current object. When no desigcountries are current, subobjects of the current object are initialized in order according to the kind of the present object: array aspects in raising subscript order, structure members in declaration order, and the first named member of a union.<....>