Dear programmers, I have a problem with writing programs. The essence of the problem: I do not understand what the error is in f3, it produces: [Error] invalid array assignment, swears at buf, in which I want to write a variable of type char from the structure. And the second problem: I need to sort the structure by group number, but it simply replaces, rather than sorts. How to sort a structure, NOT using sort or something like that?

#include <iostream> #include <conio.h> #include <stdlib.h> #include <stdio.h> #include <string.h> #include <locale.h> using namespace std; const int N=3; struct student {char fam[15]; int kurs; char grup[3]; float stip; }; void f1(student); void f2(student parm[]); void f3(student parm[]); int main() { setlocale(LC_ALL, "rus"); int i; float maxs; student stud[N]; //function 2 printf("\n rabota f2\n"); f2(stud); getch(); maxs=0; for(i=0;i<N;i++) if(stud[i].stip>maxs) maxs=stud[i].stip; printf("\n stud - max stipendija %f rub ",maxs); for(i=0; i<N; i++) if(stud[i].stip==maxs) printf ("\n%s",stud[i].fam); getch(); //function f1 printf("\n rabota f1\n"); for (i=0; i<N;i++) f1(stud[i]); getch(); //function f3 printf("\n rabota f3\n"); f3(stud); getch(); } void f2(student parm[]){ int i; for(i=0;i<N;i++) { printf("%d # Ñòóäåíò",i+1); printf("\nÔàìèëèÿ:"); cin>>parm[i].fam; printf("Êóðñ:"); cin>>parm[i].kurs; printf("Ãðóïïà:"); cin>>parm[i].grup; printf("Ñòèïåíäèÿ:"); cin>>parm[i].stip; } } void f1(student parm) { cout<<parm.fam<<" "<<parm.kurs<<" "<<parm.grup<<" "<<parm.stip<<"\n"; } void f3(student parm[]){ int i,j=0, k; // ñ÷åò÷èê öèêëîâ è òåêóùèé èíäåêñ ýëåìåíòà ìàññèâà char buf[3]; cout<<"\nÑîðòèðîçêà ìàññèâà ìåòîäîì \"ïóçûðüêà\"\n"; for(i=0;i<N;i++) cout<<parm[i].grup<<" "; for (i = 0; i < N-1; i++) { j++; for (k =0; k < N-1; k++) { if (parm[k].grup < parm[k+1].grup) {// îáìåíÿåì ê-é è (ê+1)-é ýëåìåíòû buf= parm[k].grup; parm[k].grup = parm[k+1].grup ; parm[k+1].grup = buf; } } // îòëàäî÷íàÿ ïå÷àòü - ñîñòîÿíèå // ìàññèâà ïîñëå î÷åðåäíîãî öèêëà ñîðòèðîâêè cout<<"\n-----"<<j<<"-----"; for (k = 0; k < N; k++) cout<<parm[k].grup<<" "; cout<<"\n"; } printf("\n massiv otsortirovan\n"); for (i=0; i<N;i++) f1(parm[i]); getch(); } 
  • one
    I think that you forgot to attach the code, telepathy, of course, we possess, but not to that extent. - KoVadim
  • You're right! Corrected .. - The Hobbit

2 answers 2

See it.

In languages ​​like C / C ++, you cannot copy complex things (like arrays) by simple assignment. Your code

 buf= parm[k].grup 

trying to do just that.

Since you are writing (apparently) in pure C, you should use the built-in memory copy :

 memcpy(/*куда*/ buf, /*откуда*/ parm[k].grup, /*сколько*/ 3); 

Why not strcpy ? Because you have fixed-length strings, which means that they have the right not to end in \0 .

Regarding sorting: this is your bubble sort, right? Now your exchange code only exchanges a group, and you have to exchange a whole student! Your best bet is to write a separate swap(student* s1, student* s2) helper function swap(student* s1, student* s2) that will exchange structure values. And in it to exchange not only the group, but also the rest of the field. (At the same time, the text of the program will become more visible.)

  • one
    @ The Hobbit, (a small hint for implementing swap() ) but the structures (unlike arrays) can be copied by an assignment operator (i.e. students' swap () code will be almost the same as the swap () of two numbers in the array). - avp
  • @avp: And the truth! Good trick, by the way. I completely forgot about it. - VladD
  • @avp, only if the structures are Plain Old Data :) - Arkady
  • one
    @VladD, I do not think that because of the length. IMHO is because arrays in C are very close to pointers. In fact, everywhere, except for the area where the array is defined (memory is allocated for it), the array and pointer are not distinguishable. Therefore, the operation of copying an array turns out to be somewhat miserable, in one place it can be used, but in all others it is not. The size is clearly not a hindrance. If an array (the size is fixed but not limited) is part of the structure, then it will be fine to copy (naturally, along with the rest of the structure fields). - POD - well, of course (others are simply not needed). - avp
  • one
    I did it! Just after if you need to replace the entire structure, and I replace a separate element :) - The Hobbit

Apparently, you are using an array or a "buf" link. Create a loop or function that swaps the 2 elements in the structure. In C, you cannot swap two arrays or two structures in places directly. You can sort the data inside the structure in the same way as the data outside. Use sorting that you like best.

  • I do not really understand how to properly write buf. And about sorting - in these ways he simply replaces the numbers for me, and I need him to sort the structure by position. : (( - The Hobbit