Why does
char line[10] = "1234";
work fine but
char line[10];
line = "1234";
throws an
error: incompatible types in assignment
error?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The first line works because it performs an initialization of the
chararray with data. It would be the same as:In the second example, the type of
"1234"isconst char*, since it is a pointer to a constantchararray. You’re trying to assign aconst char*to achar*, which is illegal. The correct way to assign a constant (or other) string to a string variable is to usestrcpy,strncpy, or any other string handling function.