误访问静态存储区的数据并企图修改时发生Access Violation错误
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
//UpperToLower.
char* UpperToLowerCase(char* buff)
{
char* p = buff;
if(p == 0)
return buff;
do{
if(*p >= 'A' && *p <= 'Z')
*p += 32;
}while(*p++ != '\0');
return buff;
}
int main()
{
char* p = "AGSDFEERER"; //Error.//char p[] = "AGSDFEERER "; //OK.
//char* p = (char*) malloc(sizeof("AGSDFEERER")); //OK.
//strcpy(p,"AGSDFEERER");
printf("p = %s\n", UpperToLowerCase(p));
return 0;
}
char *p = "AGSDFEERER "; //error,因为p指向的是存储在静态存储区中的字符串,不能修改,企图修改就会发生Access Violation 的错误。
char p[] =
"AGSDFEERER "; //OK,此时字符串存储在栈中,可以修改。
方式三:
char* p =
(char*) malloc(sizeof("AGSDFEERER"));
strcpy(p,"AGSDFEERER");//OK,此时将字符串存储到新申请的一个堆里面,可以进行修改。