代码改变世界

第二次作业

2017-03-22 17:43  htua  阅读(191)  评论(0编辑  收藏  举报
#include<stdio.h>
#include<conio.h>
void swap(int *x,int *y)
{
 int c=*x;
    *x = *y;
    *y=c;
}
int main()
{
    int a = 1, b =2;
    swap(&a,&b);
    printf("%d %d\n", a, b);
    getch();
    return 0;
}

2.编译通过,运行卡住了,百度了一下,发现新的指针是任意指向其他地址,但是内存的值应该是不能改变的。

3.传入后新建立令了形参命名的指针,只实现了形参指针的指向互换,所以执行后无法交换。

#include<stdio.h>
#include<stdlib.h>
int comp(int *p,int *q)
{
    if(*p>*q)
    {
        return *p;
    }
    else
    {
        return *q;
    }
    
}
int main()
{
    int a=4,b=2;
    int *p;int *q;
    p=&a;q=&b;
    
    printf("%d,%d\n",a,b);
    comp(p,q);
    printf("%d\n",comp(p,q));
    system("pause");
    return 0;
}

 

 

4,2
4
请按任意键继续. . .
1.两个地方的comp(p,q)编辑时没有把把他修改导致 程序运行错误,
2.重新试验了一下 ,程序运行成功