指针问题
用指针求a,b之和c并交换a,b的值,哪里不对?#include <stdio.h>
int main()
{
int a,b,c,*p ;
scanf("%d,%d",&a,&b);
p=sum ;
*p(a,b,c);
p=swap ;
*p(a,b);
printf("sum=%d\n",c);
printf("a=%d,b=%d\n",a,b);
}
sum(int a,int b,int c)
{ c=a+b ; }
swap(int a,int b)
{ int t ;
t=a ;
a=b ;
b=t ;
}
#include <stdio.h>
int sum(int * a , int * b)
{
int t ;
t = * a ;
* a = * b ;
* b = t ;
return * a + * b ;
}
int main()
{
int a , b ;
scanf("%d%d" , & a , & b) ;
printf("sum = %d\n" , sum(& a , & b)) ;
printf("a = %d , b = %d\n" , a , b) ;
}
编译、运行实况
D:\00.Excise\C>cl x.c
用于 x86 的 Microsoft (R) C/C++ 优化编译器 19.28.29334 版
版权所有(C) Microsoft Corporation。保留所有权利。
x.c
Microsoft (R) Incremental Linker Version 14.28.29334.0
Copyright (C) Microsoft Corporation.All rights reserved.
/out:x.exe
x.obj
D:\00.Excise\C>x
18 17
sum = 35
a = 17 , b = 18
D:\00.Excise\C> #include <stdio.h>
int main()
{
int sum(int *p, int *q, int *r);// 函数没有声明
int swap(int *p, int *q); // 函数没有声明
int a, b, c, *p,*q,*r;
scanf("%d%d", &a, &b);
p = &a;
q = &b;
r = &c;
sum(p, q, r);
swap(p, q);
printf("sum=%d\n", c);
printf("a=%d,b=%d\n", a, b);
}
int sum(int *p, int *q, int *r)
{
*r = *p + *q;
return *r;
}
int swap(int *p, int *q)
{
int t;
t = *p;
*p = *q;
*q = t;
return 0;
}
页:
[1]