传值
#include<stdio.h>void swap(int *x, int *y);
void swap(int *x, int *y)
{
int temp;
printf("In swap, 交换前:x = %d, y = %d\n", *x, *y);
temp = *x;
*x = *y;
*y = temp;
printf("In swap, 交换后: x = %d, y = %d\n", *x, *y);
}
int main()
{
int x = 3, y = 5;
printf("In main,交换前:x = %d, y = %d\n", x, y);
swap(&x, &y);
printf("In main, 交换后:x = %d, y = %d\n", x, y);
return 0;
}
函数定义中括号里面的是指针吗?为什么在主函数中xy不定义成指针 没必要,只要把地址传进去就行了 都定义完了为什么要在main区块中再定义?同意楼上说的,直接给地址。
页:
[1]