0

Guys tell me how can I write the following code In using Union instead of structure ?

#include <stdio.h> #include <stdlib.h> #include <string.h> union Student { char* name; int roll_number; float marks; }; int main() { int i = 0, n = 5; union Student student[n]; //Students data student[0].name = "Kapil"; student[0].roll_number = 1; student[0].marks = 73.6; student[1].name = "Vikas"; student[1].roll_number = 2; student[1].marks = 78.3; student[2].name = "Ajay"; student[2].roll_number = 3; student[2].marks = 87.4; // Print the Students information printf("Student Details:\n\n"); for (i = 0; i < n; i++) { printf("\tName = %s\n", student[i].name); printf("\tRoll Number = %d\n", student[i].roll_number); printf("\tTotal Marks = %f\n\n", student[i].marks); } return 0; }

29th Dec 2021, 4:04 AM
Mr. A
Mr. A - avatar
4 Respostas
+ 2
Instead of "union" use "struct " struct Student { struct Student student[n]; Use this instead: int i = 0, n = 3;
29th Dec 2021, 5:30 AM
SoloProg
SoloProg - avatar
+ 2
union and struct are designed for different purpose, and are not compatible nor convertible. Please review the chapters to see the difference https://www.sololearn.com/learn/C/2944/?ref=app https://www.sololearn.com/learn/C/2942/?ref=app
29th Dec 2021, 6:51 AM
Ipang
+ 1
Okkk
29th Dec 2021, 6:52 AM
Mr. A
Mr. A - avatar
0
Then how can I prove that this code is a union type ?? Bro
29th Dec 2021, 6:42 AM
Mr. A
Mr. A - avatar