#include <stdio.h>
typedef struct point
{
int xpos;
int ypos;
}Point;
void SwapPoint(Point* po1, Point* po2) {
Point temp = { (po1->xpos),(po1->ypos) };
(po1->xpos) = (po2->xpos);
(po1->ypos) = (po2->ypos);
(po2->xpos) = (temp.xpos);
(po2->ypos) = (temp.ypos);
printf("pos1에는 %d, %d\n\n", (po1->xpos), (po1->ypos));
printf("pos2에는 %d, %d", (po2->xpos), (po2->ypos));
}
int main(void) {
Point pos1 = { 2,4 };
Point pos2 = { 5,7 };
SwapPoint(&pos1, &pos2);
return 0;
}
두 구조체 변수의 주소 값을 대상으로 SwapPoint 함수를 호출하여 값을 스와핑했다.
void SwapPoint(Point * po1, Point * p02){
Point temp = *po1;
*po1 = *po2;
*po2 = temp;
}
스왑함수를 이렇게 구조체 포인터 변수가 가리키는 값끼리 통째로 이동시켜도 된다.