c语言中指针的初始化

来源:互联网 发布:软媒win10优化工具 编辑:程序博客网 时间:2024/05/20 13:18

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".

0 0
原创粉丝点击