The problem is that you have to declare variables as teh correct type in order to use them:
char arr[3]
Allocates enough space for 3
char
items - and in C
char
is almost certainly ASCII which means it is 8 bits wide, so even on a 64 bit system the
arr
array occupies only three bytes of memory.
But this:
{"hi","how","are"}
is not three characters, it's three strings, which are much larger.
"hi" requires 3 bytes: 'h', 'i', and a null terminator.
"how" requires 4 bytes: 'h', 'o', 'w', and a null terminator.
"are" requires 4 bytes: 'a', 'r', 'e', and a null terminator.
In addition, you don't store the data in
arr
itself,
arr
requires three
pointer to char
values in order to access the strings, and each pointer needs 8 bytes on a 64 bit system! If it didn't work like that, you couldn't efficiently index through the strings as the "distance" between two strings would not be the same each time.
To use the string initializer, you need to declare
arr
so that it has enough space to store them, and that means declaring it as an array of
pointer to char
values:
char* arr[3] = {"hi","how","are"};