[Fixed] "error: assignment to expression with array type error" when I assign a struct field (C) – C

Photo of author
Written By M Ibrahim
abstract-class arrays compiler-construction fstring initialization

Quick Fix: To resolve the "error: assignment to expression with array type" error when assigning a struct field in C, use strcpy() to copy into the array instead of direct assignment. For example, instead of s1.name = "Paolo";, use strcpy(s1.name, "Paolo");.

The Problem:

"You are getting an error ""error: assignment to expression with array type error"" when assigning values to struct fields (s1.name and s1.surname) in C. The error occurs because you are trying to assign string literals directly to these fields, which are arrays of characters. Instead, you should assign individual characters or use string initialization."
}

The Solutions:

Solution 1: Assignment to Array Types

The issue occurs in the following statement:

s1.name="Paolo";

This is because you are trying to assign a string literal directly to an array (s1.name) which is not permitted.

According to the C11 standard, a modifiable lvalue (left-hand value) in an assignment statement must be of a type other than an array. In this case, s1.name is an array of characters, and therefore not modifiable.

To resolve this issue, you should use the strcpy() function to copy the string literal into the array:

strcpy(s1.name, "Paolo");

Solution 2: Using memcpy

The provided code defines a struct data as a block of memory that fits 60 characters plus 4 for the integer. The assignments in the main function attempt to assign pointers to strings into strings, which is not valid.

To copy "Paulo" into the struct at the point name and "Rossi" into the struct at point surname, use memcpy:

#include <string.h> // for memcpy

data s1;
memcpy(s1.name, "Paulo", 6); // copy "Paulo" into s1.name
memcpy(s1.surname, "Rossi", 6); // copy "Rossi" into s1.surname
s1.age = 1;

Alternatively, you can define a struct that points to character arrays of any length:

typedef struct {
    char *name;
    char *surname;
    int age;
} data;

In this case, the assignments in the main function will work because you are filling the struct with pointers:

data s1;
s1.name = "Paulo"; // assign the pointer to "Paulo" to s1.name
s1.surname = "Rossi"; // assign the pointer to "Rossi" to s1.surname
s1.age = 1;

Solution 3: Inside a struct a member cannot be assigned as a string literal

When we define a struct, like:

typedef struct{
         char name[N];
         char surname[N];
         int age;
    } data;

each member of the struct is, in fact, an array.
When you try assigning a literal, the compiler doesn’t know that you’re trying to assign the value to the first element of the array and it gives you a "error: assignment to expression with array type".
To solve the problem, you have to use the strcpy function which copies the string.

The correct code is:

strcpy(s1.name , &quot;Egzona&quot;);
printf( &quot;Name : %s\n&quot;, s1.name);

Solution 4: Use strcpy() function

In C, you cannot directly assign a string to a character array, as this would result in an “assignment to expression with array type” error. Instead, you need to use the `strcpy()` function to copy the string into the character array.

Here’s how you can fix your code:

“`c
#include
#include // Include the string.h header for strcpy()

#define N 30

typedef struct{
char name[N];
char surname[N];
int age;
} data;

int main() {
data s1;

strcpy(s1.name, "Paolo"); // Use strcpy() to copy the string into the character array
strcpy(s1.surname, "Rossi"); // Use strcpy() to copy the string into the character array
s1.age = 19;

getchar();
return 0;

}