I'm currently playing a bit with C and trying to understand strings. Can somebody please explain why this is working:
char test[] = "test";
And why the following does not?
char test[255];
test = "test";
Because this is an initialization:
char test[] = "test";
and this is an assignment:
test = "test";
and you cannot assign arrays in C (and strings in C are just arrays).
Your best bet is to copy the string using strcpy() (or to be safe, strncpy()).
You can't assign an array directly because it is an unmodifiable lvalue. But you can use an indirectly assignment like:
typedef struct { char s[100]; } String;
int main()
{
char a[100] = "before";
assert( sizeof a >= sizeof(String) );
puts( a );
*(String*)a = *(String*) "after";
puts( a );
return 0;
}