When does printf(“%s”, char*) stop printing?

前端 未结 4 694
天命终不由人
天命终不由人 2021-01-11 10:19

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         


        
4条回答
  •  走了就别回头了
    2021-01-11 10:33

    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

提交回复
热议问题