I am writing a small database. The structure includes the student's last name and 3 grades. All this needs to be pushed into a simply-connected linear list and work with it further. When I want to create any element of the list, the compiler outputs: "The expression must be valid for changing the left-hand value". What is the problem? Code:

struct students { char surname[20]; int marks[3]; } onestudent; struct studentlist { students a; studentlist *next; }; void main() { setlocale(LC_ALL, "Russian"); studentlist *u = NULL; //указатель на начало списка u = new studentlist; u->a.surname = scanf("%s", onestudent.surname); for (int i = 0; i < 3; i++) { u->a.marks = scanf("%d", onestudent.marks[i]); } //... } 
  • 2
    There is no new in C! - PinkTux
  • @Pink Tux yes, in malloc, but from us the teacher requires new. - Katya Gibteva
  • 2
    So, check with the teacher what he means. Either it is all about C +++, or you misunderstand it. There is nothing more to help here. Except as void * new(size_t size) { return malloc(size); } void * new(size_t size) { return malloc(size); } ... - PinkTux
  • The problem is: scanf("%s", onestudent.surname) . onestudent.surname - it’s not at all clear what is. Should be scanf("%s", &(u->a.surname)) - andy.37

2 answers 2

Well, since C ++, then do not use scanf (all the more so - wrong). In C ++, for this purpose there is std::cin :

 studentlist *u = new studentlist; u->next = NULL; std::cin >> u->a.surname; for( int i = 0; i < 3; i++ ) { std::cin >> u->a.marks[i]; } 

    scanf returns the number of items - i.e. int. And you are trying to assign this int arrays.