+ 3

In typedef struct what is name of struct here ?

typedef struct { int id; char title[40]; float hours; } course, c, cpp; In this the struct have three names or not if no then c and cpp are variable?

9th Apr 2020, 5:29 AM
ㅤㅤㅤㅤㅤㅤ
ㅤㅤㅤㅤㅤㅤ - avatar
2 Réponses
9th Apr 2020, 5:51 AM
Gordon
Gordon - avatar
+ 4
`typedef` defines type alias(es), so in your type definition code above, all the 3 <course>, <c> and <cpp> are type aliases for the defined struct. Later on you can use either one of the 3 to define a variable of that struct type. For example; #include <stdio.h> typedef struct { int id; char title[40]; float hours; } course, c, cpp; int main() { course x = { 942020, "Generic", 942.020 }; c y = { id: 123, title: "C language", hours:456 }; cpp z = { 1098, "C++", 10.98 }; printf("%d %s %.3f\n%d %s %.2f\n%d %s %.2f\n", x.id, x.title, x.hours, y.id, y.title, y.hours, z.id, z.title, z.hours); return 0; }
9th Apr 2020, 6:22 AM
Ipang