C语言中指针函数里面含有指针函数怎么编写,下面的有什么不对

#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
int a,b,c;
scanf("%d,%d,%d",&a,&b,&c);
swap3(&a,&b,&c);
printf("%d\n%d\n%d\n",a,b,c);
return 0;
}
swap2(int *p1,int *p2){
int temp;
if(*p1<*p2){
temp=*p1;
*p1=*p2;
*p2=temp;
}
}
swap3(int *p1,int *p2,int *p3){
swap2(&p1,&p2);
swap2(&p1,&p3);
swap2(&p2,&p3);
}

swap2和swap3没有写返回值


scanf里"%d,%d,%d"最好不要写逗号


main()里的参数用不到最好不写


swap2(&p1,&p2);

swap2(&p1,&p3);

swap2(&p2,&p3);

swap2的参数是指针.p1,p2,p3本身就是指针了.为什么还要&?


如果函数定义在main后要声明函数


修改如下

#include <stdio.h>
#include <stdlib.h>
void swap2(int *p1,int *p2);
void swap3(int *p1,int *p2,int *p3);
int main() {
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
swap3(&a,&b,&c);
printf("%d\n%d\n%d\n",a,b,c);
return 0;
}
void swap2(int *p1,int *p2){
int temp;
if(*p1<*p2){
temp=*p1;
*p1=*p2;
*p2=temp;
}
}
void swap3(int *p1,int *p2,int *p3){
swap2(p1,p2);
swap2(p1,p3);
swap2(p2,p3);
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-06-17
没有申请内存 new一下追问

New一下是什么,可以详细一点吗

追答

就是申请内存函数 作用如c语言malloc

相似回答