gpt4 book ai didi

c++ - 使用指针数组时编程 "has stopped working"

转载 作者:太空宇宙 更新时间:2023-11-04 15:22:58 27 4
gpt4 key购买 nike

我正在尝试创建一个字符串数组,并将它们与链表连接起来。问题是我们只能使用数组。讲师说我们不能使用模板、 vector 等。这些是我发现能够做到这一点的唯一方法。我认为这甚至不是链表,他希望我们的数组并行处理,而不是指向行中的下一个元素。

#include <iostream>
#include <string>

using namespace std;

int main ()
{
string names [4] = {"Dick", "Harry", "Sam", "Tom"};
string *nameptr[4];

for(int x = 0; x < 4; x++)
{
*nameptr[x] = names[x];
cout << nameptr[x] << " ";
cout << &nameptr[x] << endl;
}
}

这段代码有什么问题?我错过了什么?

我迷路了,如果有人能对此有所启发,那就太好了。

最佳答案

您的问题不明确,但要让您的代码运行。

尝试更新

*nameptr[x] = names[x];

nameptr[x] = &names[x];

关于c++ - 使用指针数组时编程 "has stopped working",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14886245/

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