I specified the sizes of the arrays so
char myCharArr[4] = "hello world";
assigns only the first 4 chars without '\0'.
Then memcpy copies this data "as is" to data.shortArr. So this array has only this 4 chars.
someGarbage is an arrays of 16 chars, when I assign "some garbage" there, it sets first 12 chars to represent the symbols of the string, then sets 13th char to zero ('\0'), the last three chars probably left as zero (not sure if in C they are initialized or not to zero), but we don't care at all here about these last three chars. When I memcpy the someGarbage array I copy all 16 chars "as is".
What I wanted to show, that if you print your array of 4 chars withour '\0' at the end, then it's matter of luck whether it will be printed correctly or not (as in klimi's example there can be non-zero memory after your array on the stack and then it will interpret memory as part of your string until it find a zero byte).
You don't want to rely on something that works by luck.
first time hearing matter of luck in computer science :)
Anonymous
Pavel
Gulshan
MAC
MRT
Wendy
Diego
Ammar
Ganesh