This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//901-1 | |
#include<stdlib.h> | |
#include<stdio.h> | |
void exchange(int*, int*);//宣告副程式,名字叫exchange | |
//呼叫時要傳入兩個整數的記憶體位置 | |
int main() { | |
int a, b;//宣告兩個整數 | |
printf("請輸入兩個數字(數字跟數字中間請以空白間隔)");//列印文字 | |
scanf("%d %d", &a, &b);//讓使用者輸入兩個數字 | |
printf("a:%d b:%d\n", a, b);//列印出變數 | |
exchange(&a, &b);//呼叫副程式,並且傳入a,b兩個整數的記憶體位址 | |
printf("a:%d b:%d\n", a, b);//列印出變數 | |
system("PAUSE");//停住等待使用者按下任意一個按鍵 | |
return 0;//跳出 | |
} | |
void exchange(int* a, int* b) { | |
int ch;//宣告整數 | |
ch = *a;//ch=a的記憶體位址 | |
*a = *b;//a=b的記憶體位址 | |
*b = ch;//b=ch的記憶體位址(原本a的記憶體位址) | |
//讓a和b的記憶體位址交換 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment