比如将s的内容复制到n
char *s="hello world";
char *n;
strcpy(n,s);
之后再打印n的内容,如果是printf("%s",n);的话 结果就是“hello world”
但是如果是逐个打印
for(int i=0;i<strlen(n);i++){
printf("%c",n[i]);
}
运行时会出现Segmentation fault: 11
比如将s的内容复制到n
char *s="hello world";
char *n;
strcpy(n,s);
之后再打印n的内容,如果是printf("%s",n);的话 结果就是“hello world”
但是如果是逐个打印
for(int i=0;i<strlen(n);i++){
printf("%c",n[i]);
}
运行时会出现Segmentation fault: 11
(1) 对于指针必须初始化,char * n = NULL;
(2) 没有然后了