0

Copy the following code and tell me why it's not printing the output try to correct it

#include <stdio.h> #include <stdlib.h> #include <string.h> // The student structure struct Student { char* name; int roll_number; float marks; }; int main() { int i = 0, n = 5; struct 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; }

25th Dec 2021, 3:07 PM
Mr. A
Mr. A - avatar
9 Antworten
+ 3
Here is the complete solution. With the union and struct implementations and with comments. https://code.sololearn.com/c7dXX3RBE5oi/?ref=app
25th Dec 2021, 6:35 PM
Romeo Cojocaru
Romeo Cojocaru - avatar
+ 3
In an union type structure you can't use all the fields OR all the members at once. You either use the 'name', or the 'roll_number' or the 'marks'. Take the C course and learn the differences between an union AND a struct (structure). PS: You can easily use Playground to test your code and not an online compiler (like online odb).
25th Dec 2021, 3:33 PM
Romeo Cojocaru
Romeo Cojocaru - avatar
+ 3
Mr. A Two advices for posting questions: 1. Make them specific. What you want the code to do, what it's doing instead, your difficulties, etc. Something too generic, like "fix the code" or "changes" make it too hard on those wanting to help. 2. Use them to learn. This is a learning app. Asking for ready solutions solve the question, but teach you too little. Instead, ask for information that make you solve the problem. Then you'll learn for real.
25th Dec 2021, 6:48 PM
Emerson Prado
Emerson Prado - avatar
+ 2
Mr. A So you just want the code modification... I don't think you will eventually understand WHY... but your wish is granted: Replace the Student structure from 'union' to 'struct' by writing struct keyword instead of union. And here, don't use a variable to allocate memory in stack. If you want that, you need to dynamically allocate memory in the heap, using calloc()... concepts that you will learn by taking the C course. struct Student student[5]; //not union Student student[n]; Now, your code will work. I tested in the Playground. Also, you missed two curly braces } at the end. One for the 'for' loop and one for the main() function.
25th Dec 2021, 5:36 PM
Romeo Cojocaru
Romeo Cojocaru - avatar
+ 1
Romeo Cojocaru Thanks For Solution
25th Dec 2021, 7:14 PM
Mr. A
Mr. A - avatar
0
Adding to Romeo Cojocaru answer, using the playground also allows people to readily test your code, without need to copy and paste
25th Dec 2021, 4:46 PM
Emerson Prado
Emerson Prado - avatar
0
I want to know about changes please tell me that
25th Dec 2021, 5:19 PM
Mr. A
Mr. A - avatar
0
@RomeoCojocaru thanks for your suggestion I have edited the code and added the curly braces but still not getting what are you saying. If you figure out the problem then share with me.
25th Dec 2021, 6:14 PM
Mr. A
Mr. A - avatar
0
Emerson Prado Your right but I saw many articles and find different different methods but not understand them properly that's why ask for a ready solution so I can understand where I am getting wrong. But I agree with you.
25th Dec 2021, 7:11 PM
Mr. A
Mr. A - avatar