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

前端 未结 4 750
天命终不由人
天命终不由人 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:39

    It stops printing when it reaches a null character (\0), because %s expects the string to be null terminated (i.e., it expects the argument to be a C string).

    The string literal "test\nd" is null terminated (all string literals are null terminated). Your character array ptr is not, however, because you only copy six characters into the buffer (Hello\n), and you do not copy the seventh character--the null terminator.

提交回复
热议问题