注册 登录
编程论坛 C++教室

请帮忙分析这个程序错误的原因

lindayanglong 发布于 2008-10-10 16:49, 671 次点击
请帮忙分析这个程序错的原因
#include<iostream>
using namespace std;
void swap(int *p1,int *p2)
{
    int *a;
    *a=*p1;
    *p1=*p2;
    *p2=*a;
}
void main()
{
    int a=5,b=3;
    swap(&a,&b);
    cout<<a<<" "<<b<<" "<<endl;
}
8 回复
#2
blueboy820062008-10-10 18:22
要这样写....
#include<iostream>
using namespace std;
void swap(int *p1,int *p2)
{
    int *a;
    a=p1;
    p1=p2;
    p2=a;
}
void main()
{
    int a=5,b=3;
    swap(&a,&b);
    cout<<a<<" "<<b<<" "<<endl;
}
#3
blueboy820062008-10-10 18:22
这样只是没有语法错了,,,
还不能实现你所想嘀~~
#4
blueboy820062008-10-10 18:27
#include<iostream>
using namespace std;
void swap(int *p1,int *p2)
{
    int a;
    a=*p1;
    *p1=*p2;
    *p2=a;
}
void main()
{
    int a=5,b=3;
    swap(&a,&b);
    cout<<a<<" "<<b<<" "<<endl;
}
#5
blueboy820062008-10-10 18:27
#include<iostream>
using namespace std;
void swap(int *p1,int *p2)
{
    int *a;
    a=p1;
    p1=p2;
    p2=a;
    cout<<*p1<<" "<<*p2<<" "<<endl;
}
void main()
{
    int a=5,b=3;
    swap(&a,&b);
  //  cout<<a<<" "<<b<<" "<<endl;
}
#6
blueboy820062008-10-10 18:28
不知道你想怎样换...
有很多方法...
上面列了两个,你自己想下吧...
#7
lindayanglong2008-10-10 19:30
我知道要把*a换成a就行了,但是我想知道用*a为什么不行,错的内部原因
#8
heyyroup2008-10-10 20:17
指针必须初始化吧。
int *a;
*a = *p;这里没有初始化指针就直接赋值。不妥吧。没有在编译器上跑过。不知道对不对,我只是感觉这里有点问题。
#9
woooh2008-10-11 18:50
我调进去看了下
显示是指针没赋初值造成的
1