프로그래밍[Univ]/C언어

[C언어소스] swap (void 타입 사용, general swap)

Cloud Travel 2008. 12. 18. 12:45

#include<stdio.h>

int swap(void *i, void *j, int size)
{
    int k;
    char *p, *q;
    p = i;
    q = j;
    char temp;
    for ( k = 0 ; k < size ; k++ , *p++, *q++)
    {
        temp = *p;
        *p = *q;
        *q = temp;
    }
}

typedef struct link {
    char name[10];
    int id;
} link;
int main()
{
    int i = 3 , k = 5;
    double f = 1.1 , g = 2.2 ;
    link x, y;
    scanf("%s",&x.name);;
    x.id = 111;
    scanf("%s",&y.name);;
    x.id = 333;
    printf("before\n");
    printf("i : %d k : %d \n",i,k);
    printf("f : %f g : %f \n",f,g);
    printf("x.name : %s x.id : %d \n",x.name,x.id);
    swap(&i,&k,sizeof(int));
    swap(&f,&g,sizeof(double));
    swap(&f,&g,sizeof(link));
    printf("after\n");
    printf("i : %d k : %d \n",i,k);
    printf("f : %f g : %f \n",f,g);
    printf("x.name : %s x.id : %d \n",x.name,x.id);
}

-----------------------------------------------
main은 테스트를 해본 부분입니다