string literal in c

前端 未结 6 949
刺人心
刺人心 2021-01-05 09:12

Why is the following code illegal?

typedef struct{
   char a[6];
} point;

int main()
{
   point p;
   p.a = \"onetwo\";
}

Does it have any

6条回答
  •  星月不相逢
    2021-01-05 09:51

    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);

提交回复
热议问题