正确代码如下:

#include <stdio.h>

void fun(int *x, int *y) { int t; t = *x; *x = *y; *y = t; }

void main() { int a, b; a = 8; b = 3; fun(&a, &b); printf("%d %d\n ", a, b); }

修改说明:

  1. 在函数中交换变量值时,应该使用指针来操作变量,因此应该将 x 和 y 指针所指向的值进行交换,而不是指针本身。
  2. 在交换变量值时,应该使用一个中间变量来暂存一个变量的值,以便于交换操作。
函数fun的功能是:将主函数中两个变量的值进行交换。例如若变量a中的值为8b中的值为3则程序运行后a中的值为3b中的值为8。 请改正程序中的错误使它能得出正确的结果。#include stdiohfoundvoid funint x int y int t;found t=x;x=y;y=t;void main int ab; a=8; b=3; fun&a &b; printfd

原文地址: https://www.cveoy.top/t/topic/cJAB 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录