c - how to access value of a pointer variable declared as type of struct -


I am trying to store students' record in the designated indicator indicator as a student. I have declared that a student of Porter Variable Stratified is given student_info as shown in the code and assigned to the memory to use the student value whenever we want to enter a student record. I want to use ith students and tried to enter the values ​​for that one. I tried to use the Iith elements that use the following code but it is not working properly. Whenever I try to print the stored value in the student variable, it always shows zero. Please tell me the mistake in this code and whether my method of accessing the element is correct or not.

  # include & lt; Stdio.h & gt; #to & lt include, stdlib.h & gt; Int i; Struct student_info {int id; Four names [20]; Int quiz 1; Int quiz 2; Int total_score; }; Int choice (); Int add_record (struct student_info *); Int main () {int option; Struct student_info * student; Student = (struct student_info *) malloc (struct student_info); While (1) {option = option (); If (option == 1) {add_record (student); } Else {exit (0); }} Return 0; } Int choice () {int option; Printf ("------------------------------------ \ n"); Printf ("------------------------------------ \ n"); Printf ("\ t \ tMenu \ t \ t \ n"); Printf ("------------------------------------ \ n"); Printf ("------------------------------------ \ n"); Printf ("1. Add Student Record \ n"); Printf ("0 Exit \ n") printf ("Enter your choice \ n"); Scanf ("% d", and option); Return option; } Int add_record (struct student_info * student) {if (i! = 0) {student = (struct student_info *) malloc (sizeof (struct student_info) * (i + 1)); } (Student + i) - & gt; Id = i; Printf ("Enter Student Name"); Scanf ("% s", (student + i) - & gt; name); Printf ("Enter Question 1 Issue"); Scanf ("% d", & amp; (student + i) -> quiz 1); Printf ("Enter quiz 2 points"); Scanf ("% d", & amp; (student + i) -> quiz2); (Student + i) - & gt; Total_core = (student + i) - & gt; Quiz 1 + (Student + i) - & gt; Quiz 2; I ++; Printf ("Total numeric% d \ n", * & amp; (student + i) - & gt; total_score); Return 0; }  

There are many mistakes in your code, which are the following: - 1.You There is no variable and end_score named mid_term in your straight but you are using it in your code. 2.You are allocating memory twice for the same composition pointer; One main and the other add_record function. 3. You are making a very silly mistake, you are moving before the student's score is printed, so you are receiving 0 as the final score.

Here I am assuming that there are many students who can be added to the record and can change according to your requirement. Here's your working code.

  # include & lt; Stdio.h & gt; #to & lt include, stdlib.h & gt; Int i; Struct student_info {int id; Four names [20]; Int quiz 1; Int quiz 2; Int total_score; }; Int choice (); Int add_record (struct student_info *); Int main () {int option, n = 15; Struct student_info * student; Student = (struct student_info *) Molec (sqft (student student_info) * N); While (1) {option = option (); If (option == 1) {add_record (student); } Else {exit (0); }} Return 0; } Int choice () {int option; Printf ("------------------------------------ \ n"); Printf ("------------------------------------ \ n"); Printf ("\ t \ tMenu \ t \ t \ n"); Printf ("------------------------------------ \ n"); Printf ("------------------------------------ \ n"); Printf ("1. Add Student Record \ n"); Printf ("0 Exit \ n"); Printf ("enter your choice \ n"); Scanf ("% d", & amp; option); Return option; } Int add_record (struct student_info * student) {(student + i) - & gt; Id = i; Printf ("Enter Student Name"); Scanf ("% s", (student + i) - & gt; name); Printf ("Enter Question 1 Issue"); Scanf ("% d", & amp; (student + i) -> quiz1); Printf ("Enter quiz 2 points"); Scanf ("% d", & amp; (student + i) -> quiz2); (Student + i) - & gt; Total_core = (student + i) - & gt; Quiz 1 + (Student + i) - & gt; Quiz 2; Printf ("Total numeric% d \ n", * & amp; (student + i) - & gt; total_score); I ++; Return 0; }  

Comments