假设我有两个函数:
void function1(int *ptr) {
printf("%d", *ptr);
}
和
void function2(char *str) {
printf("%s", str);
}
当 str
之前没有遵从运算符时,为什么 function2 可以工作?在 str
中只有 address 指向的不是我想的 value。
最佳答案
Why does function2 work, when there is no deference operator before str
因为标准定义的 %s
需要一个 char *
,即一个地址。
7.21.6.1
If no l length modifier is present, the argument shall be a pointer to the initial element of an array of character type. Characters from the array are written up to (but not including) the terminating null character. If the precision is specified, no more than that many bytes are written. If the precision is not specified or is greater than the size of the array, the array shall contain a null character.
关于c - 为什么 char 字符串中没有取消引用运算符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11653583/