Home > Blockchain >  expression must be a modifiable lvalue error in c
expression must be a modifiable lvalue error in c

Time:12-21

I am trying to assign empty to some strings using struct in c by this code:

void initializer(table* s)
{
    for(int i = 0 ;i<N;i  )
    {
        s[i].name= "Empty";

    }
}

and this in main function

table s[N];
initializer(s);

This is my struct :

struct table {
    char name[10];
    int marks;
} ;

typedef struct table table;

I still have to declare marks as -1 but I guess that shouldn't solve this error;

what am i doing wrong?

CodePudding user response:

You cannot assign a string to a char array. use strlcpy.

CodePudding user response:

An object can be modified with = operator only if it is an modifiable L-value. From 6.3.2.1p1:

... A modifiable lvalue 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.

The type of name member is char[10] so it cannot be assigned using = operator. Use a dedicated function strcpy:

strcpy(s[i].name, "Empty");
  • Related