我需要通过指针交换两个字符,但是当我运行这段代码时,程序崩溃了。
int main(){
char *s1 = "string1";
swap(st,(st+1));
/* BUT THIS CODE WORKS - Whats the problem?
* char s1[] = "string1";
* swap(s1,&s1[1]);
*/
return 0;
}
void swap(char * const ptr1, char * const ptr2){
char temp = *ptr1;
*ptr1 = *ptr2;
*ptr2 = temp;
}