【结构体】c++需要帮助

问题遇到的现象和发生背景

我跟着黑马学结构体敲的代码,可是总是报错,0x00007FFC977B1400 (vcruntime140d.dll)处(位于 黑马跟学.exe 中)引发的异常: 0xC0000005: 读取位置 0xFFFFFFFFFFFFFFFF 时发生访问冲突。以下是源代码

用代码块功能插入代码,请勿粘贴截图
#include<iostream>
#include<string>
using namespace std;
struct student
{
    string name;
    int score=0;
};
struct teacher
{
    string tname;

    struct student sarry[5];
};
void allocatespace(struct teacher tarry[], int len)
{
    string nameseed = "ABCDE";
    for (int i = 0; i < len; i++)
    {
        tarry[i].tname = "Teacher_";
        tarry[i].tname += nameseed[i];
        for (int j = 0; i < 5; j++)
        {
            tarry[i].sarry[j].name = "Student_";
            tarry[i].sarry[j].name += nameseed[j];
            tarry[i].sarry[j].score = 60;
        }
    }
}
void printinfo(struct teacher tarry[], int len)
{
    for (int i = 0; i++; i < len)
    {
        cout << "老师姓名" << tarry[i].tname << endl;
        for (int j = 0; j < 5; j++)
        {
            cout << "学生姓名" << tarry[i].sarry[j].name <<
                tarry[i].sarry[j].score;
        }
    }
}
int main()
{
    struct teacher tarry[3];
    int len;
    len = sizeof(tarry) / sizeof(tarry[0]);
    allocatespace(tarry, len);
    printinfo(tarry, len);
}

运行结果及报错内容

0x00007FFC977B1400 (vcruntime140d.dll)处(位于 黑马跟学.exe 中)引发的异常: 0xC0000005: 读取位置 0xFFFFFFFFFFFFFFFF 时发生访问冲突。

img

错误很多,帮你修正了

#include

#include<string.h>

using namespace std;

struct student

{

string name;

int score=0;

};

struct teacher

{

string tname;

struct student sarry[5];

};

void allocatespace(struct teacher tarry[], int len)

{

string nameseed = "ABCDE";

for (int i = 0; i < len; i++)

{

tarry[i].tname = "Teacher_";

std::string ok="ok";

tarry[i].tname += nameseed[i];

for (int j = 0; j < 5; j++)

{

tarry[i].sarry[j].name = "Student_";

tarry[i].sarry[j].name += nameseed[j];

tarry[i].sarry[j].score = 60;

}

}

}

void printinfo(struct teacher tarry[], int len)

{

for (int i = 0;i < len; i++)

{

cout << "老师姓名" << tarry[i].tname << endl;

for (int j = 0; j < 5; j++)

{

cout << "学生姓名" << tarry[i].sarry[j].name <<

tarry[i].sarry[j].score;

}

cout << endl;

}

}

int main()

{

struct teacher tarry[3];

int len;

len = sizeof(tarry) / sizeof(tarry[0]);

printf("len:%d\n",len);

allocatespace(tarry, len);

printinfo(tarry, len);

return 0;

}