Why is the following code illegal?
typedef struct{
char a[6];
} point;
int main()
{
point p;
p.a = \"onetwo\";
}
Does it have any
Arrays are non modifiable lvalues
. So you cannot assign to them. Left side of assignment operator must be an modifiable lvalue
.
However you can initialize an array when it is defined.
For example :
char a[] = "Hello World" ;// this is legal
char a[]={'H','e','l','l','o',' ','W','o','r','l','d','\0'};//this is also legal
//but
char a[20];
a = "Hello World" ;// is illegal
However you can use strncpy(a, "Hello World",20);