Why don't I need to dereference a character pointer in C before printing it?

后端 未结 6 1224
遇见更好的自我
遇见更好的自我 2020-12-13 11:19

Why does this code work? I would expect that I would need to dereference ptr, printf(\"%s\\n\", *ptr); before I could print it out, but I get a

6条回答
  •  -上瘾入骨i
    2020-12-13 12:11

    The format specifier %s tells printf to expect a pointer to a null-terminated char array. Which is what name and ptr are.

    *name and *ptr are not. If you dereference them, you'll get back a single char, which is basically lying to printf - resulting in undefined behavior.

提交回复
热议问题