In my class we are writing our own copy of C\'s malloc() function. To test my code (which can currently allocate space fine) I was using:
char* ptr = my_mal
James is correct about printf stopping when it gets to the null character, and Uri is correct that you need to allocate a 7-character buffer to hold "hello\n".
Some of the confusion with terminators would be mitigated if you used the usual C idiom for copying a string: strcpy(ptr, "Hello\n")
, rather than memcpy
.
Also, by definition, sizeof(char) == 1 in C, so 6*sizeof(char)
is redundant