swapping members in array of structs

I want to swap two structs in an array of structs: I just can't figure out how to create a temp object to store one of the objects being overwritten:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

struct Record
{
	string student_name = "";
	int student_grade = 0;
};


void bubblesort_grades(Record * n_ptr, int num_students)
{

        ...bubble sort...

        ?? = n_ptr[1];
	n_ptr[1] = n_ptr[0];
        n_ptr[0] = ??
       
	
}


int main
..
Last edited on
Well, do you know how to do it with other types? For example how would you swap a and b.
1
2
int a = 2;
int b = 5;

Yes.

In both examples I use a third int or temp value to make it work. I'm not having much luck with array of structs.
ints:

1
2
3
4
5
6
	int temp(0), a(2), b(5);
	temp = b;
	cout<<a<<" "<<b<<endl;
	b = a;
	a =temp;
	cout<<a<<" "<<b;


snippet pointers:

1
2
3
4
5
6
val_1 =*(my_array+ctr);
val_2 =*(my_array+ctr+1);
if (val_1>val_2)
{temp = val_2; val_2 = val_1; val_1 = temp;}
*(my_array+ctr)=val_1;
*(my_array+ctr+1)=val_2;
Last edited on
Assuming val_1, val_2 and temp are of type Record, the swap looks like it should work.
Other parts of the code do give me cause for concern.


Anyway, perhaps you could clarify, when you say "I'm not having much luck", could you be more specific please. If there's an error message then please post it here in full. If the code runs but doesn't work, then please say what happens.
Last edited on
Ok, Chervil you caused me to redouble my efforts on the pointer type. Analysis showed I was creating 2 pointers each for one of two struct types. Errant. So I created 2 pointers same type both pointing at 1 struct object and it worked. Now I have 2 different pointers for same struct - great!

Snippet
1
2
3
4
5
6
7
...
Record * n_ptr = new Record[num_students];
Record * n_temp; // previously Temp * n_temp
n_temp = n_ptr;
...
bubblesort_grades(n_ptr, num_students, n_temp);
...


Snippet

This snippet is a placeholder for bubblesort fx, not meant to be the actual one.
1
2
3

void bubblesort_grades(Record * n_ptr, int &num_students, Record * n_temp)
...

....
Last edited on
It sounds as though you have this working now. I hope so. But feel free to ask further questions.
1
2
3
n_ptr[0].student_grade = 95;
n_ptr[0].student_name = "Rob"
n_temp[0].student_grade = 9999;


I can assign values/strings no problem to struct.

When I want to assign the struct array value to a val the compiler complains.
1
2
Record val;
 val = n_ptr[1].student_grade;


 
.\src\main.cpp:52:10: error: no match for 'operator=' (operand types are 'Record' and 'int')

Last edited on
Edited: See below.



Last edited on
When I want to assign the struct array value to a val the compiler complains.
Record val;
val = n_ptr[1].student_grade; //error


Just val buy itself was to ambiguous.

This worked below, except it won't index( e.g. val[1], val[2] etc. Fortunately I don't need any indexing with val just for storage of one record.

 
val.student_grade = n_ptr[1].student_grade; //saves to val! 


I was able to assign struct student_grade to val by using the (.) operator for access.
Last edited on
1
2
Record val;
 val = n_ptr[1].student_grade;


Try either this (simpler) - recommended:
1
2
Record val;
 val = n_ptr[1];

or this (if for some reason you need to):
1
2
Record val;
 val.student_grade = n_ptr[1].student_grade;

Only use the second method if you need to assign just part of the values (unlikely).


A third option:
1
2
int grade;
 grade = n_ptr[1].student_grade;

Last edited on
All 3 options work fine. Thank you. I've run accross the error that prompted this discussion in the first place:

1
2
3
4
        Record val_2;
	val_2 = n_ptr[1];
	Record val_3;
	val_3 = n_ptr[1].student_grade;


I get a compilation error with val_3 = n_ptr[1].student_grade;
val_2 works fine; can access its members no prob.

error: ..\src\main.cpp:107:8: error: no match for 'operator=' (operand types are 'Record' and 'int')

If I write the code for val_3 to be the same as val_2 at the top, the error disappears:

This executes:

1
2
3
4
5
val_2 = n_ptr[1];
	Record val_3;
	val_3 = n_ptr[1];
	cout<<val_3.student_grade<<endl;
	cout<<val_2.student_grade<<endl;


But val_3 = n_ptr[1].student_grade; won't.
The reason why this won't work
val_3 = n_ptr[1].student_grade;
is a mismatch of types.

val_3 is a Record.
n_ptr[1] is a Record as well
but
n_ptr[1].student_grade is an int

It is an attempt to assign a value of type int to a variable of type Record.

Not only are the types incompatible, but also, it was almost certainly not what you were intending to do, in other words the fact that an error is occurs is good, it helps you to spot your mistakes.
Topic archived. No new replies allowed.