【发布时间】:2013-12-05 08:28:26
【问题描述】:
char *p = "hello";
printf("%c",*p); //output would be ***h***
printf("%s",p); //output would be ***hello***
在第 2 行为什么我们必须使用 *p 到 print 和 char 而在第 3 行我们必须使用 p 到 print 一个字符串。
同样在第 1 行,字符串是如何存储在内存中的。
【问题讨论】:
-
const char *p = "hello"或char p[] = "hello"。char *p = "hellp"是等待发生的段错误。例如,请参阅this。 -
从
'char*p'中取出'*p'得到'char',即第一个'printf'收到一个字符,现在从'char*p'中取出'p'得到'char*',即第二个'printf'接收一个char指针,不知道是不是设计的