標題:
指標 (三)
[打印本頁]
作者:
tonyh
時間:
2014-9-13 17:07
標題:
指標 (三)
本帖最後由 tonyh 於 2014-9-13 17:31 編輯
利用指標法 (變更門牌), 將變數x與變數y的值對調.
[attach]967[/attach]
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
int x=10, y=5;
int *xPtr=&x, *yPtr=&y; //宣告指標xPtr與yPtr,同時將x與y的位址指派給它們
cout<<"[對調前]"<<endl;
cout<<"x="<<*xPtr<<endl;
cout<<"y="<<*yPtr<<endl;
cout<<"變數x的位址: "<<xPtr<<endl;
cout<<"變數y的位址: "<<yPtr<<endl<<endl;
xPtr=&y;
yPtr=&x;
cout<<"[對調後]"<<endl;
cout<<"x="<<*xPtr<<endl;
cout<<"y="<<*yPtr<<endl;
cout<<"變數x的位址: "<<xPtr<<endl;
cout<<"變數y的位址: "<<yPtr<<endl<<endl;
system("pause");
return 0;
}
複製代碼
作者:
張峻瑋
時間:
2014-9-13 17:19
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
int x=10,y=5;
int *xPtr,*yPtr;
xPtr=&x;
yPtr=&y;
cout<<"[對調前]"<<endl;
cout<<"x="<<*xPtr<<endl;
cout<<"y="<<*yPtr<<endl;
cout<<"變數x的位址: "<<xPtr<<endl;
cout<<"變數y的位址: "<<yPtr<<endl<<endl;
xPtr=&y;
yPtr=&x;
cout<<"[對調後]"<<endl;
cout<<"x="<<*xPtr<<endl;
cout<<"y="<<*yPtr<<endl;
cout<<"變數x的位址: "<<xPtr<<endl;
cout<<"變數y的位址: "<<yPtr<<endl;
system("pause");
return 0;
}
複製代碼
作者:
林宇翔
時間:
2014-9-13 17:19
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
int x = 10, y = 5;
int *xPtr,*yPtr;
xPtr=&x;
yptr=&y;
cout<<"對調前 "<<x<<endl;
cout<<"x=: "<<*xPtr<<endl;
cout<<"y=: "<<*yPtr<<endl;
cout<<"變數x的地址"<<xPtr<<endl;
cout<<"變數y的地址"<<yPtr<<endl;
xPtr=&y;
yptr=&x;
cout<<"對調後 "<<x<<endl;
cout<<"x=: "<<*xPtr<<endl;
cout<<"y=: "<<*yPtr<<endl;
cout<<"變數x的地址"<<xPtr<<endl;
cout<<"變數y的地址"<<yPtr<<endl;
system("pause");
return 0;
}
複製代碼
作者:
張郁庭
時間:
2014-9-13 17:20
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
int x=10,y=5;
int *xPtr,*yPtr;
xPtr=&x;
yPtr=&y;
cout<<"[對調前]"<<endl;
cout<<"x="<<xPtr<<endl;
cout<<"y="<<yPtr<<endl;
cout<<"變數x的位置: "<<xPtr<<endl;
cout<<"變數y的位置: "<<yPtr<<endl;
xPtr=&y;
yPtr=&x;
cout<<"[對調後]"<<endl;
cout<<"x="<<xPtr<<endl;
cout<<"y="<<yPtr<<endl;
cout<<"變數x的位置: "<<xPtr<<endl;
cout<<"變數y的位置: "<<yPtr<<endl;
system("pause");
return 0;
}
複製代碼
作者:
周雍程
時間:
2014-9-13 17:20
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
int x=10, y=5;
int *xPtr, *yPtr;
xPtr=&x;
yPtr=&y;
cout<<"[對調前]"<<endl;
cout<<"x="<<*xPtr<<endl;
cout<<"y="<<*yPtr<<endl;
cout<<"變數x的位址: "<<xPtr<<endl;
cout<<"變數y的位址: "<<yPtr<<endl;
xPtr=&y;
yPtr=&x;
cout<<"[對調後]"<<endl;
cout<<"x="<<*xPtr<<endl;
cout<<"y="<<*yPtr<<endl;
cout<<"變數x的位址: "<<xPtr<<endl;
cout<<"變數y的位址: "<<yPtr<<endl;
system("pause");
return 0;
}
複製代碼
作者:
張彥承
時間:
2014-9-13 17:21
#include<iostream>
#include<cstdlib>
#include<string>
using namespace std;
int main()
{
int x=10,y=5;
int *xPtr, *yPtr;
xPtr=&x;
yPtr=&y;
cout<<"[對調前]"<<endl;
cout<<"x= "<<*xPtr<<endl;
cout<<"y= "<<*yPtr<<endl;
cout<<"變數x的位址: "<<xPtr<<endl;
cout<<"變數y的位址: "<<yPtr<<endl;
xPtr=&y;
yPtr=&x;
cout<<"[對調後]"<<endl;
cout<<"[對調前]"<<endl;
cout<<"x= "<<*xPtr<<endl;
cout<<"y= "<<*yPtr<<endl;
cout<<"變數x的位址: "<<xPtr<<endl;
cout<<"變數y的位址: "<<yPtr<<endl;
xPtr=&y;
yPtr=&x;
system("pause");
return 0;
}
複製代碼
作者:
李允軒
時間:
2014-9-13 17:21
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
int x, y;
x = 10;
y = 5;
int *xPtr, *yPtr;
xPtr = &x;
yPtr = &y;
cout << "[對調前]" << endl;
cout << " x = :" << *xPtr << endl;
cout << " y = :" << *yPtr << endl;
cout << "變數x的地址 :" << xPtr << endl;
cout << "變數y的地址 :" << yPtr << endl;
xPtr = &y;
yPtr = &x;
cout << "[對調後]" << endl;
cout << " x = :" << *yPtr << endl;
cout << " y = :" << *xPtr << endl;
cout << "變數x的地址 :" << yPtr << endl;
cout << "變數y的地址 :" << xPtr << endl;
system("pause");
return 0;
}
複製代碼
作者:
劉得旗
時間:
2014-9-13 17:38
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
int x=10, y=5;
int *xPtr,*yPtr;
xPrt=&x;
yPrt=&y;
cout<<"[對調前]"<<endl;
cout<<"x="<<*xPtr<<endl;
cout<<"y="<<*yPtr<<endl;
cout<<"變數x的位址:"<<xPtr<<endl;
cout<<"變數y的位址:"<<yPtr<<endl;
xPtr=&y;
yPtr=&x;
cout<<"[對調後]"<<endl;
cout<<"x="<<*xPtr<<endl;
cout<<"y="<<*yPtr<<endl;
cout<<"變數x的位址:"<<xPtr<<endl;
cout<<"變數y的位址:"<<yPtr<<endl;
system("pause");
return 0;
}
複製代碼
作者:
劉得恩
時間:
2014-9-17 13:08
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
int x=10,y=5;
int* xPtr=&x, *yPtr=&y;
cout<<"[對調前]"<<endl;
cout<<"x="<<*xPtr<<endl;
cout<<"y="<<*yPtr<<endl;
cout<<"變數x的位址: "<<xPtr<<endl;
cout<<"變數y的位址: "<<yPtr<<endl<<endl;
xPtr=&y;
yPtr=&x;
cout<<"[對調後]"<<endl;
cout<<"x="<<*xPtr<<endl;
cout<<"y="<<*yPtr<<endl;
cout<<"變數x的位址: "<<xPtr<<endl;
cout<<"變數y的位址: "<<yPtr<<endl<<endl;
system("pause");
return 0;
}
複製代碼
作者:
陳品叡
時間:
2024-8-9 16:32
#include <bits/stdc++.h>
using namespace std;
int main()
{
int x = 10, y = 100, *xptr = &x, *yptr = &y;
cout << "x=" << *xptr << endl;
cout << "y=" << *yptr << endl;
cout << "x位置: " << xptr << endl;
cout << "y位置: " << yptr << endl;
swap(xptr ,yptr);
cout << "x=" << *xptr << endl;
cout << "y=" << *yptr << endl;
cout << "x位置: " << xptr << endl;
cout << "y位置: " << yptr << endl;
return 0;
}
複製代碼
作者:
陳人瑀
時間:
2024-10-5 16:38
#include <bits/stdc++.h>
using namespace std;
int main()
{
int x=10,y=5;
int *xptr=&x ,*yptr=&y;
cout<<x<<" "<<y<<endl;
cout<<&x<<" "<<&y<<endl;
xptr= &y;
yptr= &x;
cout<<*xptr<<" "<<*yptr<<endl;
cout<<&x<<" "<<&y<<endl;
return 0;
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/)
Powered by Discuz! 7.2