When creating a function that adds students to an array of structures, highlights red.
Here is the whole code
# include & lt; stdio.h & gt;
#Include & lt; String.h & gt;
#Include & lt; locale.h & gt;
Struct AddStudent (Char Name [], Unsigned Int Age, Unsigned Int Course, Unsigned Int);
STRUCT STUDENT {
Char Name [50];
unsigned int age;
unsigned int course;
};
unsigned int student_counter = 0;
INT MAIN (Void) {
SETLOCALE (LC_ALL, "RUS");
STRUCT STUDENT ST [20];
STRCPY_S (ST [0] .Name, 50, "Ruslan");
ST [0] .AGE = 18;
ST [0] .course = 1;
Student_counter + = 1;
For (int i = 0; i & lt; student_counter; i ++) {
PrintF ("% s \ t% d Course \ t% d years \ n", St [i] .name, ST [I] .COURSE, ST [I] .AGEAGE);
}
_Getch ();
Return 0;
}
Struct AddStudent (Char Name [], Unsigned Age, Unsigned Course, Unsigned Age) {
Student Res_ST;
SCRCPY_S (RES_ST, 50, NAME);
}
Why can’t create an addstudent function? Thanks in advance.
Answer 1
You would have to study pointers and allocation of memory
Here is the working example:
# include & lt; stdio.h & gt;
#Include & lt; stdlib.h & gt;
#Include & lt; String.h & gt;
#Include & lt; locale.h & gt;
Struct Student * AddStudent (Char * Name, Unsigned Int Age, Unsigned Int Course);
STRUCT STUDENT {
Char Name [50];
unsigned int age;
unsigned int course;
};
unsigned int student_counter = 0;
INT MAIN (Void) {
SETLOCALE (LC_ALL, "RUS");
STRUCT STUDENT * ST [20];
ST [0] = addstudent ("Ruslan", 18, 1);
Student_counter + = 1;
For (int i = 0; i & lt; student_counter; i ++) {
PrintF ("% s \ t% d Course \ t% d years \ n", ST [I] - & GT; NAME, ST [I] - & GT; Course, ST [I] - & gt; age);
}
Return 0;
}
Struct Student * AddStudent (Char * Name, Unsigned Int Age, Unsigned Int Course) {
struct Student * Res_ST = Malloc (Sizeof (Struct Student));
STRCPY (RES_ST- & GT; NAME, NAME);
RES_ST- & GT; AGE = AGE;
RES_ST- & GT; Course = Course;
RETURN RES_ST;
}
and the addstudent function you can’t create because:
-
In the poundation arguments after unsigned, you need to write a data type int
-
You do not return the structure you created, it is created inside the function and after its completion it disappears
-
The arguments are duplicated by Age