那里错了?为什么数值没换过来?
来源:百度知道 编辑:UC知道 时间:2024/05/23 20:10:40
#include "stdafx.h"
#include "iostream.h"
void change(int*,int*,int*);
void main()
{ int m,n,p;
cout<<"please input 3 numbers"<<endl;
cin>>n; cin>>m ; cin>>p;
change(&n,&m,&p);
cout<<n<<" "<<m<<" "<<p<<" "<<endl;
}
void change ( int *a,int *b, int *c)
{
int *temp1, *temp2;
temp1=b; b=a;
temp2=c; c=temp1;
a=temp2;
}
#include "iostream.h"
void change(int*,int*,int*);
void main()
{ int m,n,p;
cout<<"please input 3 numbers"<<endl;
cin>>n; cin>>m ; cin>>p;
change(&n,&m,&p);
cout<<n<<" "<<m<<" "<<p<<" "<<endl;
}
void change ( int *a,int *b, int *c)
{
int *temp1, *temp2;
temp1=b; b=a;
temp2=c; c=temp1;
a=temp2;
}
void change ( int *a,int *b, int *c)
{
int *temp1, *temp2;
temp1=b; b=a;
temp2=c; c=temp1;
a=temp2;
}
指针作为函数参数不会改变指针的值,所以不能用指针交换值,应该改为:
void change ( int *a,int *b, int *c)
{
int *temp1, *temp2;
*temp1=*b; *b=*a;
*temp2=*c; *c=*temp1;
*a=*temp2;
}
change函数本身就有问题