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

后端 未结 6 1249
遇见更好的自我
遇见更好的自我 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条回答
  •  忘掉有多难
    2020-12-13 12:19

    This is because the %s format specifier in the format string you pass to printf means that the corresponding argument should be a string, not a single character. And in C, a string is a pointer to the beginning of a block of characters that has a null character (byte with a value of 0) at the end.

    Basically, this works because you're doing exactly what you're supposed to in order to print a string.

提交回复
热议问题