0

I am new to c, I have a question about structures (see the next code):

typedef struct student{
  char name[20];
  int grade;
} student1;

typedef struct group_project{
  char name_of_group[20];
  struct group_project *next;  // points to the next group
  student1 *student1;           //head of the group
} group_project;
  1. What is the difference between student and student1 (what is the name of the struct?). Can you use the same name?
  2. Why do you need to add "struct" when using group_project but you dont add struct when using the student struct?
  3. Can you use a structure without "typedef"?
  • 1. first one is the name of the type, second one is the variable name. yes. 2. because it's not typedef'd yet. 3. yes. – Shark Feb 24 '20 at 16:17
  • 1
    https://stackoverflow.com/questions/1675351/typedef-struct-vs-struct-definitions – Mat Feb 24 '20 at 16:18
  • 1
    Note that the first typedef specifies the name `student1` (with a digit); the type `student` in the second struct is not the same type — it isn't clear what type it is, but it isn't the `struct student` type based on the info in the question. This might just be a typo in the question — if so, please fix it. – Jonathan Leffler Feb 24 '20 at 16:21

2 Answers2

3

student1 is an alias for the type struct student.

And inside the group_project structure the type-alias group_project doesn't exist yet, so you have to use struct group_project.

And in C you can't use the structure tag name (like e.g. student) without the keyword struct. The whole type is struct student.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Thank you for your answer, so basicaly the compiler replaces "student1" with "struct student"? is it ok to just give them the same name (like in group_project?) –  Feb 24 '20 at 16:21
  • @mark Something like that. And it's okay to use the same structure tag name and type-alias name. – Some programmer dude Feb 24 '20 at 16:52
  • @mark "*so basically the compiler replaces "student1" with "struct student"?*" Yes, but only if `student1` is a `typedef` alias as you did it in the example in the question. If you only define `student1` by f.e.: `struct student{...}student1` then you can´t, since `student1` is then an object of the structure itself. "*is it ok to just give them the same name (like in group_project?)*" - Yes, they reside in different name spaces. – RobertS supports Monica Cellio Feb 24 '20 at 17:32
0

What is the difference between student and student1 (what is the name of the struct?). Can you use the same name?

student is the structure tag or the "name" of the structure itself and student1 is an typedefd alias of type struct student. Yes, they can have the same name or identifier because they reside in different name spaces.

Why do you need to add "struct" when using group_project but you don´t add struct when using the student1 structure?

In C, as opposed to C++, when you define an object of a structure, and didn´t typedefd the structure or defined the object at the declaration of the structure itself, you have to precede the structure tag by the keyword struct at the declaration of the respective object, like for example:

struct student student_x;         // `struct` keyword required.

Since student1 is already typedefd before, at the declaration of the structure student, you don´t need to use the struct keyword anymore, when you defining an object of that structure type.

As opposed to that, group_project as type of the pointer next in the project_group structure needs to be preceded by the keyword struct because the typedef of group_project is defined thereafter in the source code and isn´t valid at this point of time inside the structure.

Can you use a structure without "typedef"?

Yes, of course. Some user amongst the community even say this is even more appreciated because it makes your code more readable. I recommend to always use the structure by itself and not blending with any typedef, even if it requires a little bit more key typing, just to keep your code a little bit clearer, but this is a matter of opinion and style. Here you can make you your own image of it:

typedef struct vs struct definitions

Why should we typedef a struct so often in C?