I have been sitting on this problem for 3 hours and I would appreciate it if you could guide me, We have the student structure that contains a name, and points to an array of pointers of size 3 only and it should point to 3 different teachers (random of course), when I try to return the pointer to the random array from teacherArray, within the operation AttemptTeacherToStudents, in practice, nothing is received, And the pointer remains empty. I would really appreciate the help
My structs:
typedef struct
{
char name[20];
struct ClassRoom * myClass;
}Teacher;
typedef struct {
char * name;
struct ClassRoom *myClassRoom;
struct Teacher *myTeachers[3];
}Student;
int InitalizeStudent2(Student *student, char *name, Teacher *teacher)
{
student->name= name;
*student->myTeachers = teacher;
printf("%s", name);
return 0;
}
The problem is with this code:
Teacher ** teacherArray(Teacher * teachers)
{
Teacher *randomArray[3];
int i = 0;
while (i<3)
{
Teacher * teacher=teachers + (rnd() % 6);
randomArray[i++] = teacher;
}
return randomArray;
}
void AttempTeacherToStudents(Teacher* teachers)
{
Student * allOfMYStudents = (Student *)malloc(5 * sizeof(Student));
for (int i = 0; i < 5; i++)
{
char name[255];
sprintf(name, "Student_%d", 100 + i);
char *newName = _strdup(name);
InitalizeStudent2(&allOfMYStudents[i], newName, *teacherArray(teachers));
}
}
Teacher** InitalizeGenrealTeacher()
{
char allTeachers[6][6] = { "Teach1\0", "Tech2\0", "Tech3\0","Tech4\0", "Tech4\0", "Tech5\0" };
Teacher * myTeachers =(Teacher*) calloc(6 , sizeof( Teacher));
for (int i = 0; i < 6; i++)
{
InitalizeTeacher(&myTeachers[i], allTeachers[i]);
}
return myTeachers;
}
int main()
{
srand(time(NULL));
Teacher*pMyTeacher = InitalizeGenrealTeacher();
for (int i = 0; i < 6; i++)
{
printf("%s \n", pMyTeacher[i].name);
}
AttempTeacherToStudents(pMyTeacher);
}
the problem is, why the pointer of "myTeachers" is empty at [1][2]? why does cell number [0] not null too?