jeudi 2 janvier 2020

Unable to Initialize char array

Im trying to see why the following code does not work:

void test2() {
    char ulu[256];
    ulu[]{ "Hello","Hello" };

}

But this code works:

struct my_test
{
    char test_array[256];
};

void test() {
    my_test test_array[]{ "hello", "hello" };
    printf("%s", test_array);
}

I would expect both codes to fail but only the first one does

Can someone explain to me why it works when declaring and initializing the variable test_array as an object of the my_test class?

Im getting an error stating:

a value of type "const char *" cannot be used to initialize an entity of type "char"

Thanks in advance for the help!

Aucun commentaire:

Enregistrer un commentaire