C语言 |
您所在的位置:网站首页 › 函数调用英文 › C语言 |
函数调用-swap交换举例
在C语言学习中,发现不使用指针而直接调用函数容易出现错误。 先附上不使用指针的代码及结果: #include void swap(int i,int j){ int temp; temp=i;i=j;j=temp; printf("swap中的i=%d,j=%d\n",i,j); } int main(){ int i = 4,j = 6; swap(i,j); printf("i=%d,j=%d\n",i,j); }输出结果: 接下来附上使用指针的代码及结果: #include void swap(int *p1,int *p2) { int temp; temp=*p1; *p1=*p2; *p2=temp; } int main() { int a,b; while(scanf("%d %d",&a,&b)!=EOF){ printf("交换前a,b的值为:"); printf("a=%d,b=%d\n",a,b); swap(&a,&b); printf("交换后a,b的值为:"); printf("a=%d,b=%d\n",a,b); } return 0; }输出结果: |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |