I am annoyed by this... It really would be logical if:
char string[10] = "hello";
should give the same result as:
char string[10] = {0};
string = "hello"; // doesn't work!
But I guess it just doesn't work like that. Anyways, here was my workaround:
char string[10] = {0};
sprintf(string,"hello");
its almost as good, because it is short.
Strangely enough, another thing which has worked for me (though a little off-topic perhaps) is when you declare arrays of structs, you can initialize them with the good-ole double quotes like this:
struct myStruct {
char name[NAX_NAME_LEN];
};
struct myStruct name[DATA_ARRAY_SIZE];
name[1] = (struct myStruct ) { "Hello" };
Incidentally, this method of initialization is known as a "compound literal"
Id love to see if anyone could explain why this works to use double quotes and not the string = "hello"; way...
This method is great if you have a lot of strings by the way, because it allows you to write code like:
#define DATA_ARRAY_SIZE 4
enum names{ BOB, GEORGE, FRANK, SARAH};
struct myStruct {
char name[NAX_NAME_LEN];
};
struct myStruct name[DATA_ARRAY_SIZE];
name[BOB ] = (struct myStruct ) { "Bob" };
name[GEORGE] = (struct myStruct ) { "George" };
name[FRANK ] = (struct myStruct ) { "Frank" };
name[SARAH ] = (struct myStruct ) { "Sarah" };
Or if you're going to go all multilingual for some app:
#define NUM_LANGUAGES 4
enum languages{ ENGLISH , FRENCH , SWEDISH , ITALIAN };
struct myStruct {
char intro[NAX_NAME_LEN];
};
struct myStruct name[DATA_ARRAY_SIZE];
intro[ENGLISH ] = (struct myStruct ) { "Hello" };
intro[FRENCH ] = (struct myStruct ) { "Bonjour" };
intro[SWEDISH ] = (struct myStruct ) { "Hej" };
intro[ITALIAN ] = (struct myStruct ) { "Ciao" };