How to pass a structure into a function to print using pointers and dynamic memory [C-Programming] -
I am trying to exceed a structure in the structure that will print the content, Ask for
Structure
Structure student {char * fname} Char * lname; Float score; }; Create prototype
Zero print-students (Structured student ** s, input student_size); The main function
int main () {int base_number, i; Int student; Structure student * ptr; Printf ("Enter the number of students:"); Scanf ("% d", and base_number); PTR = (Structured Student *) Molec (Base_Number * Sizoff (Structure Student)); Students = base_number; Printf ("\ nEnter Student Information \ nExample: John Smith98.50 \ n \ n"); (I = 0; i & lt; base_number; i ++) for {printf ("student% d:", i + 1); scanf ("% s% s% f", & amp; (ptr + i) -> fname, & amp; (ptr + i) -> lname, & amp; (ptr + i) - & gt; ; Score); } Print Students (and PTR, Students); Return 0; } Print Student Function
Zero print students (Structured student ** s, int student_size) {int i; Struct student * temp = (Structured student *) malloc (student_size * sizeof (Structure student)); For (i = 0; i & lt; student_size; i ++) printf ("% s% s% .2f \ n", (temp + i) -> fname, (temp + i) - & gt; ; Lname, (temp + i) - & gt; score); }
structure member fname and lname < / Code> are signs of characters, are not character array, therefore, scanf call scanf ("% s% s% f", & amp; (ptr + I) -> fname, & amp; (ptr + i) -> lname, & amp; (ptr + i - - & gt; score); is incorrect and calls for undefined behavior. You need to create those members as arrays.
#define MAX_LEN 40 stent student {char fname [MAX_LEN]; Four Lim [MAX_LEN]; Float score; }; In addition, when you pass an array in a function, it reduces an indicator to its first element. You can call at that address operator & amp; should not use the results of malloc Why do you allocate memory in the printstudents function? This is the reason for the memory leak because you do not free it. Also, the function parameter s is not required to type Structure Student ** . I suggest the following changes - #include & lt; Stdio.h & gt; # Include & lt; Stdlib.h & gt; #define MAX_LEN 40 strat student {char fname [MAX_LEN]; Four Lim [MAX_LEN]; Float score; }; Zero print-students (Structured student * s, int student_size); Int main (zero) {int base_number, i; Int student; Structure student * ptr; Printf ("Enter the number of students: \ n"); Scanf ("% d", and base_number); Ptr = malloc (base_number * sizeof * ptr); Students = base_number; Printf ("\ nEnter Student Information \ nExample: John Smith98.50 \ n \ n"); For (i = 0; i & lt; base_number; i ++) {printf ("student% d: \ n", i + 1); Scanf ("% s% s% f", (ptr + i) -> fname, (ptr + i) -> lname, (ptr + i - - gt; score); } Print Students (PTR, Students); Free (PTR); Return 0; } Zero print students (Structured student * s, int student_size) {int i; (i = 0; i & lt; student_size; i ++) printf ("% s% s% .2f \ n", (s + i) - & gt; fname, (s + i) -> gt; lname , (S + i) - & gt; score); }
Comments
Post a Comment