C语言处理指针时的分段错误
可能重复:C语言处理指针时的分段错误,c,string,pointers,C,String,Pointers,可能重复: 我的代码: void strrev(char *str) { char temp, *end_ptr; /* If str is NULL or empty, do nothing */ if( str == NULL || !(*str) ) return; end_ptr = str + strlen(str) - 1; /* Swap the chars */ while( end_ptr > str ) { tem
我的代码:
void strrev(char *str) {
char temp, *end_ptr;
/* If str is NULL or empty, do nothing */
if( str == NULL || !(*str) )
return;
end_ptr = str + strlen(str) - 1;
/* Swap the chars */
while( end_ptr > str ) {
temp = *str;
*str = *end_ptr;
*end_ptr = temp;
str++;
end_ptr--;
}
}
void main() {
char temp;
char* x = "Hel2313lo123";
//temp = *x;
// strReverse(x);
strrev(x);
printf("\n%s", x);
}
而函数strrev()实际上是直接从以下内容复制的:
每当我尝试运行此程序时,就会出现分段错误。为什么会这样
谢谢大家!
char*x=“Hel2313lo123”代码>表示只读C字符串。您需要数组charx[]=“Hel2313lo123”
将char*x
更改为char x[]
,然后好好阅读,谢谢。。。我不知道!:)虽然修正是正确的,但解释是相当模糊和不准确的…@alk考虑到OP的水平,我认为这已经足够了。但是是的,这不仅仅是不准确的。