0

I have several structures, and I keep getting a warning message. I've been trying for several hours to kick it out, but I can't. I would really appreciate the help.

This is the Student struct in the Student.h header:

   #ifndef __STUDENT_H__
   #define __STUDENT_H__
   
   #include "Teacher.h"
   #include "ClassRoom.h"
   struct Teacher;
   struct ClassRoom;

   typedef struct {
       char * name;
       struct ClassRoom *myClassRoom;
       struct Teacher *myTeachers[3];
   } Student;

   void setTeacherDynamic(struct Teacher *t, struct Teacher* tt);

and this is the Student.c source code:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "Teacher.h"
#include "Student.h"

struct Teacher;

void setTeacherDynamic(Teacher *t, Teacher* tt)
{
    strcpy(t->name, tt->name);

    t->myClass = NULL;

}

The warning message is:
enter image description here The problem is with the function setTeacherDynamic(), and I don't know how to fix it.

The Teacher struct:

enter image description here

and this is when I'm calling to the function, also in Student.c

enter image description here

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Bubi
  • 27
  • 4
  • when im removing it, the warnings are still exist :( – Bubi Aug 14 '22 at 12:43
  • The error message is clear, your formal parameters are different with declaration. Check your declaration and formal parameters. – yupe Aug 14 '22 at 12:47
  • Note that you should not, in general, create function, variable, tag or macro names that start with an underscore. Part of [C11 §7.1.3 Reserved identifiers](https://port70.net/~nsz/c/c11/n1570.html#7.1.3) says: — _All identifiers that begin with an underscore and either an uppercase letter or another underscore are always reserved for any use._ — _All identifiers that begin with an underscore are always reserved for use as identifiers with file scope in both the ordinary and tag name spaces._ See also [What does double underscore (`__const`) mean in C?](https://stackoverflow.com/q/1449181) – Jonathan Leffler Aug 14 '22 at 22:03
  • You see the `__STUDENT_H__` notation in system headers because they _must_ use that notation. You should not use that notation in your headers because it is reserved for the system to use. – Jonathan Leffler Aug 14 '22 at 22:04

1 Answers1

1

You have multiple problems:

The first is that since you have forward declarations in Student.h you don't need to include Teacher.h or ClassRoom.h. And in the source file Student.c you can remove the forward declaration you have.

The second problem is that you don't actually have a struct Teacher. You have a Teacher type-alias for an anonymous structure. You should declare the actual structure for the forward declarations to work:

typedef struct Teacher { ... } Teacher;

You should do these changes for all your source and header files.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • It actually works! am I suppose to remove the included from "Student.c" too? – Bubi Aug 14 '22 at 12:47
  • 1
    @Bubi No, keep the includes in the source file. – Some programmer dude Aug 14 '22 at 12:48
  • In addition, I wanted to ask, why did I have to define the teacher like that? In addition, the way I happen to the function and do the casting to "Struct student *, is it still correct in your opinion? – Bubi Aug 14 '22 at 12:50
  • 1
    @Bubi If you don't give a name to the structure then it doesn't have a name. If a structure doesn't have a name, then it's of course not possible to use it anywhere, like in a forward declaration. Note that `struct Teacher` and `Teacher` are two *aliases* for the same structure, but they are still different. You cant forward declare a type-aliased structure without the structure name. – Some programmer dude Aug 14 '22 at 12:59
  • So why doesn't the compiler force me to write like this even for, say, a student's structure? – Bubi Aug 14 '22 at 13:25
  • @Bubi Because it's not always needed. Sometimes anonymous structures are useful. When doing forward declarations they can't be used though. – Some programmer dude Aug 14 '22 at 13:54
  • So, if i got another struct, that include 2 pointer, "ClassRoom" - student, teacher, How do you suggest to do it? – Bubi Aug 14 '22 at 15:16