c++ - Returning a pointer to the youngest student in a dynamic array -
i trying write function iterate through dynamic array holds students records in format:
name id age
once function finds youngest age, return pointer main function , output it.
the problem function of type student, structure; , formatted so:
struct student{ string name; string id; int age;
};
i struggling return pointer (ptr) , output console, outputting (what think) memory location...
here code far,
any advice helpful.
the structure:
struct student{ string name; string id; int age;
};
the function call:
cout << "youngest: " << youngest(student_dynamic, sizeof) << endl;
and function:
student* youngest(student* s, int size) { int tempage = 0; int youngestage = 100; student *pt = new student; (int = 0;i < size;i++) { tempage = (s+i)->age; if (tempage < youngestage) { youngestage = tempage; pt->age = youngestage; } } return pt; //here trying return pointer outputs youngest age //to console window... }
update:
this question has been answered 'billy pilgrim'
thankyou advice!
you returning pointer, being printed. should like:
student * s = youngest(student_dynamic, sizeof); cout << "youngest: " << s->name << endl;
(not sure sizeof
is:).
Comments
Post a Comment