ID #3519

指针实现交换两个数字的大小

#include <stdlib.h>
#include <stdio.h>
void swap(int *x,int *y)
{
    int tmp;
    tmp = *x;
    *x = *y;
    *y = tmp;

}
int main(void)
{   
    int *x,*y;
    x = (int *)malloc(sizeof(int));
    y = (int *)malloc(sizeof(int));
    /*check if malloc is successful*/
    if(x == NULL || y == NULL)
    {
        return -1;
    }
    else
    {
        printf("The address x pointed to is %d\n",x);
        printf("The address y pointed to is %d\n",y);
        *x = 1;
        *y = 2;
        swap(x,y);/*call function*/
        printf("The address x pointed to is %d\n",x);
        printf("The address y pointed to is %d\n",y);
        printf("x is %d \n",*x);
        printf("y is %d \n",*y);
        free(x);
        free(y);
        x = NULL;
        y = NULL;
        int a = 100;
        int b = 200;
        swap(&a,&b);/*call function*/
        printf("a is now %d.\n",a);
        printf("b is now %d.\n",b);
        return 0;
    }
}


2011-07-01 17:35
阅读:
I'm VC , Just U know Y
本站部分文章来源于互联网,版权归原作者所有。

延伸阅读:

C语言实现的XML解析器

C语言实现GBK/GB2312/五大码之间的转换

Linux下实时定时器在C语言中的实现和应用

c语言中返回值和返回指针的问题

利用c语言编制cgi实现搜索