I'm trying to understand c strings and pointers better. I was playing with strcpy
, and I expected the code below to cause a segmentation fault, but to my surprise, it did not.
char a [] = "hello there";
char b [] = "yay";
strcpy(b, a);
puts(a);
puts(b);
Result:
hello there
hello there
When strcpy
gets to the second "l" in "hello", shouldn't the string pointed to by b
be out of memory slots? I would have thought that after the third byte, strcpy
would run out of allocated memory in the b
array, start writing to unallocated memory, or memory allocated to another pointer, and cause a segmentation fault.
I'd appreciate help understanding this better. Thanks!