標題:
指標 (八) - 參數傳遞
[打印本頁]
作者:
tonyh
時間:
2014-9-20 16:29
標題:
指標 (八) - 參數傳遞
本帖最後由 tonyh 於 2014-9-20 16:48 編輯
利用傳遞變數指標的方式,將參數由主函式傳遞至自訂函式.
#include<iostream>
#include<cstdlib>
using namespace std;
void swap(int *, int *);
int main()
{
int x=10, y=5;
cout<<"[對調前]"<<endl;
cout<<"x="<<x<<endl;
cout<<"y="<<y<<endl;
cout<<"變數x的位址: "<<&x<<endl;
cout<<"變數y的位址: "<<&y<<endl<<endl;
swap(&x,&y);
cout<<"[對調後]"<<endl;
cout<<"x="<<x<<endl;
cout<<"y="<<y<<endl;
cout<<"變數x的位址: "<<&x<<endl;
cout<<"變數y的位址: "<<&y<<endl<<endl;
system("pause");
return 0;
}
void swap(int *a, int *b)
{
int tmp;
tmp=*a;
*a=*b;
*b=tmp;
}
複製代碼
作者:
張峻瑋
時間:
2014-9-20 16:42
#include<iostream>
#include<cstdlib>
using namespace std;
void swap(int*,int*);
int main()
{
int x=10, y=5;
cout<<"[對調前]"<<endl;
cout<<"x="<<x<<endl;
cout<<"y="<<y<<endl;
cout<<"變數x的位址: "<<&x<<endl;
cout<<"變數y的位址: "<<&y<<endl<<endl;
swap(&x,&y);
cout<<"[對調後]"<<endl;
cout<<"x="<<x<<endl;
cout<<"y="<<y<<endl;
cout<<"變數x的位址: "<<&x<<endl;
cout<<"變數y的位址: "<<&y<<endl<<endl;
system("pause");
return 0;
}
void swap(int*a,int*b)
{
int tmp;
tmp=*a;
*a=*b;
*b=tmp;
}
複製代碼
作者:
劉得恩
時間:
2014-9-20 16:43
#include<iostream>
#include<cstdlib>
using namespace std;
void swap(int*,int*) ;
int main()
{
int x=10,y=5;
cout<<"[對調前]"<<endl;
cout<<"x="<<x<<endl;
cout<<"y="<<y<<endl;
cout<<"變數x的位址: "<<&x<<endl;
cout<<"變數y的位址: "<<&y<<endl<<endl;
swap(&x,&y);
cout<<"[對調後]"<<endl;
cout<<"x="<<x<<endl;
cout<<"y="<<y<<endl;
cout<<"變數x的位址: "<<&x<<endl;
cout<<"變數y的位址: "<<&y<<endl<<endl;
system("pause");
return 0;
}
void swap(int* x,int* y)
{
int tmp=*x;
*x=*y;
*y=tmp;
}
複製代碼
作者:
周雍程
時間:
2014-9-20 16:43
#include<iostream>
#include<cstdlib>
using namespace std;
void swap(int *, int *);
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;
swap(&x,&y);
cout<<"[對調後]"<<endl;
cout<<"x="<<x<<endl;
cout<<"y="<<y<<endl;
cout<<"變數x的位址: "<<&y<<endl;
cout<<"變數y的位址: "<<&x<<endl;
system("pause");
return 0;
}
void swap(int*a, int*b)
{
int tmp;
tmp=*a;
*a=*b;
*b=tmp;
}
複製代碼
作者:
李允軒
時間:
2014-9-20 16:43
#include<iostream>
#include<cstdlib>
using namespace std;
void swap(int *, int *);
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;
swap(&x,&y);
cout<<"[對調後]"<<endl;
cout<<"x="<<x<<endl;
cout<<"y="<<y<<endl;
cout<<"變數x的位址: "<<&x<<endl;
cout<<"變數y的位址: "<<&y<<endl<<endl;
system("pause");
return 0;
}
void swap(int *a, int *b)
{
int tmp;
tmp=*a;
*a=*b;
*b=tmp;
}
複製代碼
作者:
張彥承
時間:
2014-9-20 16:43
#include<iostream>
#include<cstdlib>
using namespace std;
void swap(int *,int *);
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;
swap(&x,&y);
cout<<"[對調後]"<<endl;
cout<<"x="<<x<<endl;
cout<<"y="<<y<<endl;
cout<<"變數x的位址: "<<&x<<endl;
cout<<"變數y的位址: "<<&y<<endl<<endl;
system("pause");
return 0;
}
void swap(int *a,int *b)
{
int tmp;
tmp=*a;
*a=*b;
*b=tmp;
複製代碼
作者:
劉得旗
時間:
2014-9-20 16:44
#include<iostream>
#include<cstdlib>
using namespace std;
void swap(int *,int *);
int main()
{
int x=10, y=5;
cout<<"[對調前]"<<endl;
cout<<"x="<<x<<endl;
cout<<"y="<<y<<endl;
cout<<"變數x的位址: "<<&x<<endl;
cout<<"變數y的位址: "<<&y<<endl<<endl;
swap(&x,&y);
cout<<"[對調後]"<<endl;
cout<<"x="<<x<<endl;
cout<<"y="<<y<<endl;
cout<<"變數x的位址: "<<&x<<endl;
cout<<"變數y的位址: "<<&y<<endl<<endl;
system("pause");
return 0;
}
void swap(int *a, int *b)
{
int tmp;
tmp=*a;
*a=*b;
*b=tmp;
}
複製代碼
作者:
林宇翔
時間:
2014-9-20 16:45
#include<iostream>
#include<cstdlib>
using namespace std;
void tmm(int*,int*);
int main()
{
int x=10, y=5;
cout<<"[對調前]"<<endl;
cout<<"x="<<x<<endl;
cout<<"y="<<y<<endl;
cout<<"變數x的位址: "<<&x<<endl;
cout<<"變數y的位址: "<<&y<<endl<<endl;
tmm(&x,&y);
cout<<"[對調後]"<<endl;
cout<<"x="<<x<<endl;
cout<<"y="<<y<<endl;
cout<<"變數x的位址: "<<&x<<endl;
cout<<"變數y的位址: "<<&y<<endl<<endl;
system("pause");
return 0;
}
void tmm(int*a,int*b)
{
int tmp;
tmp=*a;
*a=*b;
*b=tmp;
}
複製代碼
作者:
張郁庭
時間:
2014-9-20 16:45
#include<iostream>
#include<cstdlib>
using namespace std;
void swap(int*,int*);
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;
swap (&x,&y);
cout<<"[對調後]"<<endl;
cout<<"x="<<x<<endl;
cout<<"y="<<y<<endl;
cout<<"變數x的位址: "<<&x<<endl;
cout<<"變數y的位址: "<<&y<<endl<<endl;
system("pause");
return 0;
}
void swap(int *a, int *b)
{
int tmp;
tmp=*a;
*a=*b;
*b=tmp;
}
複製代碼
作者:
陳品叡
時間:
2024-8-11 11:30
#include <bits/stdc++.h>
using namespace std;
int mod = 1000000007, n;
vector <int> dp;
vector <int> coin;
vector <int> visited;
int aa(int x)
{
if(visited[x]) return dp[x];
for(int i=0; i<n; i++)
{
if(coin[i]<=x)
{
dp[x] += aa(x-coin[i]);
dp[x]%=mod;
}
}
//cout << x << " " << dp[x] << endl;
visited[x]++;
return dp[x];
}
void sswap(int *, int *);
int main()
{
/*
int x;
cin >> n >> x;
dp.resize(x);
visited.resize(x);
coin.resize(n);
dp[0]++;
visited[0]++;
for(int i=0; i<n; i++)
cin >> coin[i];
cout << aa(x);
return 0;
*/
int x = 1, y = 2;
cout<<"[對調前]"<<endl;
cout<<"x="<<x<<endl;
cout<<"y="<<y<<endl;
cout<<"變數x的位址: "<<&x<<endl;
cout<<"變數y的位址: "<<&y<<endl<<endl;
sswap(&x,&y);
cout<<"[對調後]"<<endl;
cout<<"x="<<x<<endl;
cout<<"y="<<y<<endl;
cout<<"變數x的位址: "<<&x<<endl;
cout<<"變數y的位址: "<<&y<<endl<<endl;
}
void sswap(int *a, int *b)
{
swap(*a, *b);
}
複製代碼
作者:
陳品叡
時間:
2024-8-11 11:32
#include <bits/stdc++.h>
using namespace std;
int mod = 1000000007, n;
vector <int> dp;
vector <int> coin;
vector <int> visited;
int aa(int x)
{
if(visited[x]) return dp[x];
for(int i=0; i<n; i++)
{
if(coin[i]<=x)
{
dp[x] += aa(x-coin[i]);
dp[x]%=mod;
}
}
//cout << x << " " << dp[x] << endl;
visited[x]++;
return dp[x];
}
void sswap(int &, int &);
int main()
{
/*
int x;
cin >> n >> x;
dp.resize(x);
visited.resize(x);
coin.resize(n);
dp[0]++;
visited[0]++;
for(int i=0; i<n; i++)
cin >> coin[i];
cout << aa(x);
return 0;
*/
int x = 1, y = 2;
cout<<"[對調前]"<<endl;
cout<<"x="<<x<<endl;
cout<<"y="<<y<<endl;
cout<<"變數x的位址: "<<&x<<endl;
cout<<"變數y的位址: "<<&y<<endl<<endl;
sswap(x, y);
cout<<"[對調後]"<<endl;
cout<<"x="<<x<<endl;
cout<<"y="<<y<<endl;
cout<<"變數x的位址: "<<&x<<endl;
cout<<"變數y的位址: "<<&y<<endl<<endl;
}
void sswap(int &x, int &y)
{
swap(x, y);
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/)
Powered by Discuz! 7.2