如何將字符串數組的內容複製到結構中?獲取錯誤,無法將類型字符串轉換爲字符串類型。最後一個循環是我遇到麻煩的地方。我是否也需要在堆上爲字符串數組分配空間?我把它分配給了分數。我以爲一個字符串是一個真正的字符數組,所以我很困惑如何使用指針來引用和傳輸它們。如何將字符串數組複製到結構中
#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'指向現有的數組,而是試圖複製 –