C 傳值和傳位址

2021-06-19 20:21:47 字數 957 閱讀 2891

1、交換變數x和y的值

void swap(int x,int y)

int temp;

temp=x;

x=y;

y=temp;

cout<

int a=5;int b=10;

int x=1;int y=2;

void swap(int x,int y)

int a=5;int b=10;

int *x=&a;int *y=&b;//指標引用

void swap(int *x,int *y){

int temp;

temp=*x;

*x=*y;

*y=temp;

cout<

cout<

void main(){

cout<

cout<

swap(&a,&b);

cout<

cout<

cout<

呼叫swap後,x仍指向a,y仍指向b。

4、交換變數x和y的值

void swap(int * x,int * y)

int * temp;

temp=x;

x=y;

y=temp;

cout<

int a=5;int b=10;

int *x=&a;int *y=&b;//指標引用

void swap(int *x,int *y){

int *temp;

temp=x;

x=y;

y=temp;

cout<

cout<

void main(){

cout<

cout<

swap(&a,&b);

cout<

cout<

cout<

swap

函式中改變了指標的指向。

傳位址和傳值

using system using system.data using system.configuration using system.collections using system.web using system.web.security using system.web.ui usin...

C 傳值與傳位址

include struct object 傳值方式 void text1 object a 傳位址方式 void text2 object a int main text1 obj text2 obj return 0 1.傳值方式函式的形參是傳入的實參的乙份拷貝,形參是函式的區域性變數,只在函式...

傳值 傳引用 傳位址

1.值傳遞 形參是實參的拷貝,改變形參的值並不會影響外部實參的值。從被呼叫函式的角度來說,值傳遞是單向的 實參 形參 引數的值只能傳入,不能傳出。當函式內部需要修改引數,並且不希望這個改變影響呼叫者時,採用值傳遞。void swap int a,int b 呼叫 int x,y swap x,y 實...