I've seen the couple other question regarding this topic, but none of the answers have worked for me.
My swapClasses method follows:
void swapClasses(struct ClassInfo *p1, struct ClassInfo *p2){
ClassInfo *temp = p1;
*p1 = *p2;
*p2 = *temp;
}
But when I execute the code and attempt to add a class that should technically appear first in the array, it is just added to the end. No swapping takes place even though I know that portion of the code executes (tested with a simple print statement). I believe there is something wrong with the way I'm using pointers. Can anyone advise?
Getting a pointer to an element in the array won't save that element from being overwritten by a write through another pointer to it. Therefore your swap function is wrong. You should be copying the first item into the temporary, like this:
void swapClasses(struct ClassInfo *p1, struct ClassInfo *p2){
ClassInfo temp = *p1;
*p1 = *p2;
*p2 = temp;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.