- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- int testArray1()
- {
- int a[10];
- int *iptr;
- iptr =a;
- *iptr = 5;
- return *iptr;
- }
- void swap1(int *x, int *y)
- {
- int temp;
- temp =*x;
- *x =*y;
- *y = temp;
- return;
- }
- int swap2(void *x, void *y, int size)
- {
- void *temp;
- if((temp = malloc(size)) == NULL)
- {
- return -1;
- }
- memcpy(temp,x,size);
- memcpy(x,y,size);
- memcpy(y,temp,size);
- free(temp);
- return 0;
- }
- int main(void)
- {
- /*
- // 演示数组的使用
- int ta1 = testArray1();
- printf("testArray1 = %d \\n",ta1);
- */
- /*
- //演示交换两个int变量
- int a = 1;
- int b = 2;
- printf("(a,b) = (%d,%d) \\n",a,b);
- swap1(&a,&b);
- printf("(a,b) = (%d,%d) \\n",a,b);
- */
- /*
- //演示
- char a = 'a';
- char b = 'b';
- printf("(a,b) = (%c,%c) \\n",a,b);
- swap2(&a,&b,sizeof(a));
- printf("(a,b) = (%c,%c)",a,b);
- */
- return 0;
- }
- //该片段来自于http://www.codesnippet.cn/detail/1609201410434.html
来源: http://www.codesnippet.cn/detail/1609201410434.html