gpt4 book ai didi

C++:交换指向的变量

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:24:17 24 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
Why do I get a segmentation fault when writing to a string?

我有以下程序:

#include <iostream>
using namespace std;

void reverseString(char* first, char* last)
{
while(first < last)
{
cout << *first << " " << *last << endl; //for debugging; prints 'H' and 'o' then crashes
char temp = *last;
*last = *first; //this line crashes the program
*first = temp;
first++;
last--;
}
}

int main()
{
char* s = "Hello";
reverseString(s, s + strlen(s) - 1);
cout << s << endl;
}

但是,我在交换指针指向的值时遇到了问题。我认为 *p = *p1 应该只是将 p 的指向值设置为 p1 的指向值,但似乎有些问题。在此先感谢您的帮助!

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com