字符串的两种声明方式

char *s="Golden Global View";
中的"Golden Global View"是字符串常量,是存储在数据段,和int a = 5中的5类似.

const char str[] = "Golden Global View";
中的const是告诉给编译器str中的数据不能修改.数据是存储在堆栈段的.但是还是可以修改的.

详情,可以参考帖子:
http://www.programfan.com/club/post-240796.html

在该帖子中有以下内容:

//a.c
#include <stdio.h>
#include <string.h>

int main()
{
        char *s="Golden Global View";
        char *l="lob";
        char *p;        
        p=strstr(s,l);
        *p='\0';
        printf("%s",s);
}

想输出lob前面的字符串,也就是Golden G  
但是运行时出错,调试时也说有错误
原因是:
char *s="Golden Global View";是分配在常量区得,不能修改 ( *p='\0';)!
改成char s[] = "Golden Global View";

posted on 2007-07-05 16:09  坚强地活着  阅读(1327)  评论(0编辑  收藏  举报

导航