接口封装与实现

#include <stdlib.h>
2 #include <string.h>
3
4 int swap(void *x, void *y, int size)
5 {
6 void *temp;
7
8 if((temp = malloc(size)) == NULL)
9 return -1;
10 memcpy(temp, x, size); memcpy(x, y, size); memcpy(y, temp, size);
11 free(temp);
12 return 0;
13 }

#include <stdio.h>
2 #include "swap.h"
3
4 int main(int argc, char *argv[])
5 {
6 int a = 1, b = 2;
7
8 printf("%d, %d\n", a, b);
9 swap(&a, &b);
10 printf("%d, %d\n", a, b);
11 return 0;
12 }
 

猜你喜欢

转载自blog.csdn.net/iyy123IUU/article/details/88607579