c++结构体做函数参数使用地址传递出现问题

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

学校正在做毕设项目,每名teacher带领5个学生,总共有3名teacher,需求如下
设计学生和teacher的结构体,其中在teacher的结构体中,有teacher姓名和一个存放5名学生的数组作为成员
学生的成员有姓名、考试分数,创建数组存放3名teacher,通过函数给每个teacher及所带的学生赋值
最终打印出teacher数据以及teacher所带的学生数据。

问题相关代码,请勿粘贴截图
#include<iostream>;
using namespace std;
#include<string>;
#include<ctime>;

struct Student {
    string name;
    int score;

};
struct Teacher {
    string name;
    struct Student sArray[5];
};

void printTeacher(struct Teacher tArray[], int  len) {
    for (int i = 0; i < len; i++) {
        cout <<"老师的姓名为"<< tArray[i].name << endl;
        for (int j = 0; j < 5; j++) {
            cout << "\t学生的姓名为:" << tArray[i].sArray[j].name<<" 学生的成绩为:" << tArray[i].sArray[j].score << endl;
        }
    }
}

void allocateSpace(struct Teacher * tArray[], int* len) {
    string nameseed = "ABCDE";
    string tname = "教师";
    string sname = "教师";
    for (int i = 0; i < *len; i++) {
        tArray[i]->name = tname + nameseed[i];
        for (int j = 0; j < 5; j++) {
            tArray[i]->sArray[j].name = sname + nameseed[j];
            tArray[i]->sArray[j].score = rand() % 61 + 40;
        }
    }
}
int main() {
    srand((unsigned int)time(NULL)); //随机数种子 头文件 #include <ctime>
    struct Teacher tArray[3];
    int len = sizeof(tArray) / sizeof(tArray[0]);
    cout << tArray << endl;

    allocateSpace(tArray, &len);
    printTeacher(tArray, len);

    system("pause");
    return 0;
}

img

img

运行结果及报错内容

调用allocateSpace方法时它提示实参与形参不兼容

img

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

把指针去掉,使用值传递没有错误
在实参前加入&依然出错

我想要达到的结果

应该如何修改代码在使用指针的情况下达到目的,为什么我这样写会出错
修改改成值传递结果如下:

img

img


#include<iostream>之后多了分号;
using namespace std;
#include<string>之后多了分号;
#include<ctime>之后多了分号;

tArray只是数组,不是指针
void allocateSpace(struct Teacher * tArray[], int* len) {
改成
void allocateSpace(struct Teacher tArray[], int* len) {
并且
tArray[i]->name
改成
tArray[i].name

tArray[i]->sArray[j].name
tArray[i]->sArray[j].score 
改成
tArray[i].sArray[j].name
tArray[i].sArray[j].score 

你题目的解答代码如下:

void allocateSpace(struct Teacher tArray[], int* len) {
    string nameseed = "ABCDE";
    string tname = "教师";
    string sname = "教师";
    for (int i = 0; i < *len; i++) {
        tArray[i].name = tname + nameseed[i];
        for (int j = 0; j < 5; j++) {
            tArray[i].sArray[j].name = sname + nameseed[j];
            tArray[i].sArray[j].score = rand() % 61 + 40;
        }
    }
}

如有帮助,请点击我的回答下方的【采纳该答案】按钮帮忙采纳下,谢谢!

img

#include<iostream>
using namespace std;
#include<string>
#include<ctime>

struct Student {
    string name;
    int score;
};
struct Teacher {
    string name;
    struct Student sArray[5];
};

void printTeacher(struct Teacher tArray[], int  len) {
    for (int i = 0; i < len; i++) {
        cout <<"老师的姓名为"<< tArray[i].name << endl;
        for (int j = 0; j < 5; j++) {
            cout << "\t学生的姓名为:" << tArray[i].sArray[j].name<<" 学生的成绩为:" << tArray[i].sArray[j].score << endl;
        }
    }
}

void allocateSpace(struct Teacher tArray[], int* len) {
    string nameseed = "ABCDE";
    string tname = "教师";
    string sname = "教师";
    for (int i = 0; i < *len; i++) {
        tArray[i].name = tname + nameseed[i];
        for (int j = 0; j < 5; j++) {
            tArray[i].sArray[j].name = sname + nameseed[j];
            tArray[i].sArray[j].score = rand() % 61 + 40;
        }
    }
}
int main() {
    srand((unsigned int)time(NULL)); //随机数种子 头文件 #include <ctime>
    struct Teacher tArray[3];
    int len = sizeof(tArray) / sizeof(tArray[0]);
    cout << tArray << endl;

    allocateSpace(tArray, &len);
    printTeacher(tArray, len);

    system("pause");
    return 0;
}

您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632