char amessage[] = "nw is the time"; /* 定义一个数组 */
char *pmessage = "now is the time"; /* 定义一个指针*/
如果试图修改pmessage中的内容,其结果是未定义的
<span style="font-size:18px;">#include <stdio.h>
#include <malloc.h>
#include <string.h>
typedef struct S
{
int a;
char b;
char *str;
}S;
void Fun(S* s, char *p)
{
s->str = "abcdefg";
p = s->str;
return;
}
int main(int argc)
{
S *s = (struct S*) malloc(sizeof(S));
char *p;
p = "p in Main!";
s->str = "s->str in Main";
Fun(s, p);
printf("p: %s\n",p);
printf("s->str: %s\n", s->str);
return 0;
}</span>
输出:
lang@lang:~/c$ gcc test.c
lang@lang:~/c$ ./a.out
p: p in Main!
s->str: abcdefg
注意! s->str,输出的不是: "s->str in Main".