如何将字符串数组的内容复制到结构中?获取错误,无法将类型字符串转换为字符串类型。最后一个循环是我遇到麻烦的地方。我是否也需要在堆上为字符串数组分配空间?我把它分配给了分数。我以为一个字符串是一个真正的字符数组,所以我很困惑如何使用指针来引用和传输它们。如何将字符串数组复制到结构中
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
struct StudentRecords
{
string* namesRec;
int** examsptr;
};
void main()
{
const int NG = 4;
string names[] = { "Amy Adams", "Bob Barr", "Carla Carr",
"Dan Dobbs", "Elena Evans" };
int exams[][NG] =
{
{ 98,87,93,88 },
{ 78,86,82,91 },
{ 66,71,85,94 },
{ 72,63,77,69 },
{ 91,83,76,60 }
};
string *nameHolder = nullptr;
StudentRecords *data = new StudentRecords();
data->examsptr = new int*[NG];
for (int i = 0; i < NG; ++i)
{
data->examsptr[i] = new int[NG];
}
for (int count = 0; count < NG; count++)
{
for (int count2 = 0; count2 < NG; count2++)
{
(*data).examsptr[count][count2] = exams[count][count2];
cout << (*data).examsptr[count][count2] << " " << exams[count][count2] << endl;
}
}
for (int count3 = 0; count3 < 5; count3++)
{
*nameHolder = names[count3];
(*data).namesRec[count3] = *nameHolder;
cout << (*data).namesRec[count3] << endl;
}
Btw。 '(* data).'与'data->'相同' – bytecode77
'nameHolder'是一个空指针,然后你写'* nameHolder =' –
为什么没有'StudentRecords'指向现有的数组,而是试图复制 –