char *str="hello word";//在.data ==>字符串常量不允许改变(str所存储的字符串不可改变)
static char str1[20]="hello word";//.data str ==>字符串变量(str1所存储的字符串可改变)
char str2[20]="hello word";//stack;str2 ==>字符串变量(str2所存储的字符串可改变)
下面分别给出例子说明
char *GetString()
{
char *str="tulun hello";//.data的字符串常量(const性),不允许被修改
return str;
}
int main()
{
char *str=GetString();
printf("%s",str);//ok
strcpy(str,"yhping");//error
printf("%s",str);
return 0;
}
char *GetString()
{
static char str[]="tulun hello";//.data的字符数组
return str;
}
int main()
{
char *str=GetString();
printf("%s\n",str);//ok
strcpy(str,"yhping");//ok
printf("%s\n",str);
return 0;
}
char *GetString()
{
char *str="tulun hello";//.data的字符串常量(const性),不允许被修改
return str;
}
int main()
{
char *str=GetString();
printf("%s",str);//error
strcpy(str,"yhping");//error
printf("%s",str);
return 0;
}