陌路呀 2022-04-09 21:37 采纳率: 100%
浏览 71
已结题

跟随B站黑马程序员写结构体案例一是引出的0xC0000005

#include<iostream>
using namespace std;
struct stu 
{
    string sname;
    int score;
};
struct teacher
{
    string tname;
    struct stu sarray[5];
};
void allocatespace(struct teacher tarray[],int len)
{
    string nameseed = "ABCDE";
    for (int i = 0;i < len;i++)
    {
        tarray[i].tname = "teacher_";
        tarray[i].tname += nameseed[i];
        for (int j = 0;j < 5;i++)
        {
            srand((int)time(0));
            tarray[i].sarray[j].sname = "student_";
            tarray[i].sarray[j].sname += nameseed[j];
            tarray[i].sarray[j].score = rand() % 100;
        }
    }
}
void print(struct teacher tarray[], int len)
{
    for (int i = 0;i < len;i++)
    {
        cout <<"第" << i + 1 << "老师的姓名:" << tarray[i].tname << endl;
        for (int j = 0;j < len;j++)
        {
            cout << "他的第" << j + 1 << "个学生的姓名是:" << tarray[i].sarray[j].sname << "他的成绩是:" << tarray[i].sarray[j].score << endl;
        }
    }
}
int main()
{
    struct teacher tarray[3];
    int len = sizeof(tarray) / sizeof(tarray[0]);
    allocatespace(tarray, len);
    print(tarray, len);
    system("pause");
    return 0;
}

0x00007FFECFC71400 (vcruntime140d.dll)处(位于 Project1.exe 中)引发的异常: 0xC0000005: 读取位置 0xFFFFFFFFFFFFFFFF 时发生访问冲突。
总是显示在 struct teacher tarray[3]; 这一行出问题

我的解答思路和尝试过的方法

根据网上用了逐行运行确定出错位置并将结构体初始化,还是没有作用

我想要达到的结果

修改后正常运行

  • 写回答

2条回答 默认 最新

  • CSDN专家-link 2022-04-09 21:43
    关注

    第20行
    for (int j = 0;j < 5;i++)
    这里应该是j++
    另外一个就是 srand((int)time(0));应该放到main函数里,不能放到for循环里,否则每次的随机数是一样的

     #include<iostream>
    using namespace std;
    struct stu 
    {
        string sname;
        int score;
    };
    struct teacher
    {
        string tname;
        struct stu sarray[5];
    };
    void allocatespace(struct teacher tarray[],int len)
    {
        string nameseed = "ABCDE";
        for (int i = 0;i < len;i++)
        {
            tarray[i].tname = "teacher_";
            tarray[i].tname += nameseed[i];
            for (int j = 0;j < 5;j++)
            {
    
                tarray[i].sarray[j].sname = "student_";
                tarray[i].sarray[j].sname += nameseed[j];
                tarray[i].sarray[j].score = rand() % 100;
            }
        }
    }
    void print(struct teacher tarray[], int len)
    {
        for (int i = 0;i < len;i++)
        {
            cout <<"第" << i + 1 << "老师的姓名:" << tarray[i].tname << endl;
            for (int j = 0;j < len;j++)
            {
                cout << "他的第" << j + 1 << "个学生的姓名是:" << tarray[i].sarray[j].sname << "他的成绩是:" << tarray[i].sarray[j].score << endl;
            }
        }
    }
    int main()
    {
        srand((int)time(0));
        struct teacher tarray[3];
        int len = sizeof(tarray) / sizeof(tarray[0]);
        allocatespace(tarray, len);
        print(tarray, len);
        system("pause");
        return 0;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 4月17日
  • 已采纳回答 4月9日
  • 创建了问题 4月9日

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改