简单C++指针问题扼要 求教!!

来源:百度知道 编辑:UC知道 时间:2024/09/12 15:47:21
#include <iostream>
using namespace std;
void swap(int &x, int &y);
int main()
{
int x=3, y=4;
swap(x,y);
cout << "x = " << x << ", y = " << y << endl;
return 0;
}
void swap(int &x, int &y)
{
int t = x;
x = y;
y = t;
}

函数功能:将X, Y相互转换!!

要求: 将swap函数改用指针实现

我很烦恼,请高手帮帮忙!!
new int 是什么意思啊?

常常晕倒 的这个可不行啊,还内存泄漏。。。

#include <iostream>
using namespace std;
void swap(int *x, int *y);
int main()
{
int x=3, y=4;
swap(&x,&y);
cout << "x = " << x << ", y = " << y << endl;
return 0;
}
void swap(int *x, int *y)
{
int t = *x;
*x = *y;
*y = t;
}

#include <iostream>
using namespace std;
void swap(int *x, int *y);
int main()
{
int x=3, y=4;
swap(x,y);
cout << "x = " << x << ", y = " << y << endl;
return 0;
}
void swap(int *x, int *y)
{
int *t =new int ;
t=x
x = y;
y = t;
}

#include <iostream>
using namespace std;
void swap(int *x, int *y);
int main()
{
int x=3, y=4;
int *p1,*p2;
p1=&x;p2=&y;
swap(p1,p2);
cout &l