我真的很困惑在字符串上使用指针.感觉他们遵守不同的规则.请考虑以下代码
char *ptr = "apple";// perfectly valid here not when declaring afterwards like next
ptr = "apple"; // shouldn't it be *ptr = "apple"
Run Code Online (Sandbox Code Playgroud)也printf()表现不同-
printf("%s", ptr) // Why should I send the address instead of the value
Run Code Online (Sandbox Code Playgroud)我还在一本书中看到了以下代码
char str[]="Quest";
char *p="Quest";
str++; // error, constant pointer can't change
*str='Z'; // works, because pointer is not constant
p++; // works, because pointer is not constant
*p = 'M'; // error, because string is constant
Run Code Online (Sandbox Code Playgroud)我无法理解应该暗示什么
请帮忙,我在其他地方找不到任何信息