#include <stdio.h>
void Swap(int x, int y);
int main()
{
int a, b;
printf("Please enter a,b:");
scanf_s("%d %d", &a, &b);
printf("Before swap: a = %d, b = %d\n", a, b);
Swap(a, b);
printf("After swap: a = %d, b = %d\n", a, b);
return 0;
}
void Swap(int x, int y)
{
int temp;
temp = x;
x = y;
y = temp;
}
為什么進入函式就 不能執行了哎?
uj5u.com熱心網友回復:
C語言之傳值的,需要傳地址才有效果。僅供參考:#include <stdio.h>
void Swap(int *x, int *y);
int main(void)
{
int a, b;
printf("Please enter a,b:");
scanf_s("%d %d", &a, &b);
printf("Before swap: a = %d, b = %d\n", a, b);
Swap(&a, &b);
printf("After swap: a = %d, b = %d\n", a, b);
return 0;
}
void Swap(int *x, int *y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/232784.html
標籤:C語言
上一篇:向大家請教一個演算法題
