为什么代码无误,一运行就会出现“引发了异常: 写入访问权限冲突。 **_Left** 是 0x129B30”?


/*9.3 编制程序,调用传递引用的参数,实现两个字符串变量的交换。例如开始:
char *ap=”hello”;
char *bp=”how are you?”;
交换的结果使得ap和bp指向的内容分别为:
ap:”how are you?”
bp:”hello”
*/

#include<iostream>
#include<string>
#include<math.h>
using namespace std;
void swap(char *&x,char *&y){
    char *temp;
    temp = x;
    x = y;
    y = temp;
}
void main() {
    char *ap= "hello";
    char *bp = "how are you?";
    cout << "*ap=" << ap << endl;
    cout << "*bp=" << bp << endl;
    swap(*ap,*bp);
    cout << "交换后为:" << endl;
    cout << "*ap=" <<ap<< endl;
    cout << "*bp=" <<bp<< endl;
}

swap(ap,bp),把*号去掉啊。参数要求是指针,你加 * 号后就是字符串的第一个字符了。

您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632