標題:
指標 (四)
[打印本頁]
作者:
tonyh
時間:
2014-9-13 17:24
標題:
指標 (四)
本帖最後由 tonyh 於 2014-9-13 17:31 編輯
利用傳統方式, 將變數x與變數y的值對調, 比較與指標法的差異.
指標法:
[attach]968[/attach]
傳統法:
[attach]969[/attach]
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
int x=10, y=5, tmp;
cout<<"[對調前]"<<endl;
cout<<"x="<<x<<endl;
cout<<"y="<<y<<endl;
cout<<"變數x的位址: "<<&x<<endl;
cout<<"變數y的位址: "<<&y<<endl<<endl;
tmp=x;
x=y;
y=tmp;
cout<<"[對調後]"<<endl;
cout<<"x="<<x<<endl;
cout<<"y="<<y<<endl;
cout<<"變數x的位址: "<<&x<<endl;
cout<<"變數y的位址: "<<&y<<endl<<endl;
system("pause");
return 0;
}
複製代碼
作者:
林宇翔
時間:
2014-9-13 17:30
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
int x = 10, y = 5,tmp;
cout<<"對調前 "<<x<<endl;
cout<<"x=: "<<*xPtr<<endl;
cout<<"y=: "<<*yPtr<<endl;
cout<<"變數x的地址"<<xPtr<<endl;
cout<<"變數y的地址"<<yPtr<<endl;
tmp = x;
x=y;
y=tmp;
cout<<"對調後 "<<x<<endl;
cout<<"x=: "<<x<<endl;
cout<<"y=: "<<y<<endl;
cout<<"變數x的地址"<<&x<<endl;
cout<<"變數y的地址"<<&y<<endl;
system("pause");
return 0;
}
複製代碼
作者:
張峻瑋
時間:
2014-9-13 17:30
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
int x=10,y=5,tmp;
cout<<"[對調前]"<<endl;
cout<<"x="<<x<<endl;
cout<<"y="<<y<<endl;
cout<<"變數x的位址: "<<&x<<endl;
cout<<"變數y的位址: "<<&y<<endl<<endl;
tmp=x;
x=y;
y=tmp;
cout<<"[對調後]"<<endl;
cout<<"x="<<x<<endl;
cout<<"y="<<y<<endl;
cout<<"變數x的位址: "<<&x<<endl;
cout<<"變數y的位址: "<<&y<<endl<<endl;
system("pause");
return 0;
}
複製代碼
作者:
李允軒
時間:
2014-9-13 17:31
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
int x = 10, y = 5, tmp;
cout << "[對調前]" << endl;
cout << " x = :" << x << endl;
cout << " y = :" << y << endl;
cout << "變數x的地址 :" << &x << endl;
cout << "變數y的地址 :" << &y << endl;
tmp = x;
x = y;
y = tmp;
cout << "[對調後]" << endl;
cout << " x = :" << x << endl;
cout << " y = :" << y << endl;
cout << "變數x的地址 :" << &x << endl;
cout << "變數y的地址 :" << &y << endl;
system("pause");
return 0;
}
複製代碼
作者:
周雍程
時間:
2014-9-13 17:32
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
int x=10, y=5, tmp;
cout<<"[對調前]"<<endl;
cout<<"x="<<x<<endl;
cout<<"y="<<y<<endl;
cout<<"變數x的位址: "<<&x<<endl;
cout<<"變數y的位址: "<<&y<<endl;
tmp=x;
x=y;
y=tmp;
cout<<"[對調後]"<<endl;
cout<<"x="<<x<<endl;
cout<<"y="<<y<<endl;
cout<<"變數x的位址: "<<&y<<endl;
cout<<"變數y的位址: "<<&x<<endl;
system("pause");
return 0;
}
複製代碼
作者:
劉得旗
時間:
2014-9-13 17:33
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
int x=10, y=5,tmp;
cout<<"[對調前]"<<endl;
cout<<"x="<<x<<endl;
cout<<"y="<<y<<endl;
cout<<"變數x的位址:"<<&x<<endl;
cout<<"變數y的位址:"<<&y<<endl;
tmp=x;
x=y;
y=tmp;
cout<<endl;
cout<<"[對調後]"<<endl;
cout<<"x="<<x<<endl;
cout<<"y="<<y<<endl;
cout<<"變數x的位址:"<<&x<<endl;
cout<<"變數y的位址:"<<&y<<endl;
system("pause");
return 0;
}
複製代碼
作者:
張郁庭
時間:
2014-9-13 17:34
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
int x=10, y=5, tmp;
cout<<"[對調前]"<<endl;
cout<<"x="<<x<<endl;
cout<<"y="<<y<<endl;
cout<<"變數x的位址: "<<&x<<endl;
cout<<"變數y的位址: "<<&y<<endl<<endl;
tmp=x;
x=y;
y=tmp;
cout<<"[對調後]"<<endl;
cout<<"x="<<x<<endl;
cout<<"y="<<y<<endl;
cout<<"變數x的位址: "<<&x<<endl;
cout<<"變數y的位址: "<<&y<<endl<<endl;
system("pause");
return 0;
}
複製代碼
作者:
張彥承
時間:
2014-9-13 17:36
#include<iostream>
#include<cstdlib>
#include<string>
using namespace std;
int main()
{
int x=10,y=5,tmp;
cout<<"[對調前]"<<endl;
cout<<"x= "<<x<<endl;
cout<<"y= "<<y<<endl;
cout<<"變數x的位址: "<<&x<<endl;
cout<<"變數y的位址: "<<&y<<endl;
tmp=x;
x=y;
y=tmp;
cout<<"[對調前]"<<endl;
cout<<"x= "<<x<<endl;
cout<<"y= "<<y<<endl;
cout<<"變數x的位址: "<<&x<<endl;
cout<<"變數y的位址: "<<&y<<endl;
system("pause");
return 0;
}
複製代碼
作者:
劉得恩
時間:
2014-9-17 13:14
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
int x=10,y=5, tmp;
cout<<"[對調前]"<<endl;
cout<<"x="<<x<<endl;
cout<<"y="<<y<<endl;
cout<<"變數x的位址: "<<&x<<endl;
cout<<"變數y的位址: "<<&y<<endl<<endl;
tmp=x;
x=y;
y=tmp;
cout<<"[對調後]"<<endl;
cout<<"x="<<x<<endl;
cout<<"y="<<y<<endl;
cout<<"變數x的位址: "<<&x<<endl;
cout<<"變數y的位址: "<<&y<<endl<<endl;
system("pause");
return 0;
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2