fun(int a, int b, int c) { c = a * b; } int main(void) { int c; fun(2, 3, c); printf("%d\n", c); return 0; }怎么修改让c有确定的值
时间: 2024-03-26 17:34:42 浏览: 34
要让c有确定的值,可以修改fun函数的参数列表,将c改为指针类型,这样fun函数中就可以通过指针修改c的值,代码如下:
```
void fun(int a, int b, int* c) {
*c = a * b;
}
int main(void) {
int c;
fun(2, 3, &c);
printf("%d\n", c);
return 0;
}
```
这里将c修改为int类型指针,通过传递指针的方式将c的地址传递给fun函数,fun函数中通过指针修改c的值,这样就可以在main函数中输出c的正确值。
相关问题
用c语言交换两个整数的值,函数接口定义:void fun (int *a,int *b);
下面是交换两个整数的值的 C 语言代码:
```c
void fun(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
```
调用这个函数时,传入两个整数的地址即可:
```c
int main() {
int a = 1, b = 2;
printf("Before swap: a = %d, b = %d\n", a, b);
fun(&a, &b);
printf("After swap: a = %d, b = %d\n", a, b);
return 0;
}
```
输出:
```
Before swap: a = 1, b = 2
After swap: a = 2, b = 1
```
#include<stdio.h> void fun (int *a,int *b); int main() { int a,b; scanf("a=%d,b=%d",&a,&b); fun(&a,&b); printf("a=%d b=%d\n",a,b); return 0; } void fun(int *a,int *b){ int temp; temp=*a; *a=*b; *b=temp; }c语言
这段代码实现了通过指针将两个变量的值进行交换的功能。首先在主函数中输入两个整数a和b,然后调用fun函数,将a和b的地址作为参数传递给fun函数。在fun函数中,将a所指向的变量值赋给temp,然后将b所指向的变量值赋给a所指向的变量,最后将temp的值赋给b所指向的变量。因此,通过指针操作,成功实现了a和b的值交换。最后在主函数中输出交换后的结果。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)