Learning to work with structures. I want to implement adding an element using the AddNewElement() function; But the program does not compile. Although if this piece of code from the function is transferred to the initialization itself ( Init() ), it works fine. Apparently the problem with the end . But how to deal with it, I can not figure it out.
#include <iostream> using namespace std; struct Student { char* name; int age; }; struct List { Student student; List* next; }; void Print(List *begin) { List* print = begin; while(print) { cout <<print->student.name<<"->"<<print->student.age<<endl; print = print->next; } cout<<"NULL"<<endl; } void AddNewElement(char* NewName, int NewAge) { end ->next = new List; end = end->next; end -> student.name = NewName; end -> student.age = NewAge; end ->next = NULL; } void Init(List **begin) { *begin = new List; (*begin) -> student.name = "Andrew"; (*begin) -> student.age = 20; (*begin) -> next = NULL; List* end = *begin; AddNewElement("Petr", 19); } int main() { List *begin = NULL; Init(&begin); Print(begin); return 0; }