First, you don't need to do all that. In particular, the strcpy
is redundant - you don't need to copy a string just to printf
it. Your message
can be defined with that string in place.
Second, you've not allowed enough space for that "Hello, World!" string (message
needs to be at least 14 characters, allowing the extra one for the null terminator).
On the why, though, it's history. In assembler, there are no strings, only bytes, words etc. Pascal had strings, but there were problems with static typing because of that - string[20]
was a different type that string[40]
. There were languages even in the early days that avoided this issue, but that caused indirection and dynamic allocation overheads which were much more of an efficiency problem back then.
C simply chose to avoid the overheads and stay very low level. Strings are character arrays. Arrays are very closely related to pointers that point to their first item. When array types "decay" to pointer types, the buffer-size information is lost from the static type, so you don't get the old Pascal string issues.
In C++, there's the std::string
class which avoids a lot of these issues - and has the dynamic allocation overheads, but these days we usually don't care about that. And in any case, std::string
is a library class - there's C-style character-array handling underneath.