如何更改作为参数传递的变量的值?如何更改C中作为参数传递的变量的值?我试过这个:void foo(char *foo, int baa){
if(baa) {
foo = "ab";
} else {
foo = "cb";
}}然后打电话:char *x = "baa";foo(x, 1);printf("%s\n", x);但它印出来了baa为什么?提前谢谢。
3 回答
不负相思意
TA贡献1777条经验 获得超10个赞
char*
foo()
char**
char
foo()
void foo(char **foo /* changed */, int baa){ if(baa) { *foo = "ab"; /* changed */ } else { *foo = "cb"; /* changed */ }}
foo()
x
&
):
foo(&x, 1);
baa
char *foo
x
x
梵蒂冈之花
TA贡献1900条经验 获得超5个赞
void foo(char *foo, int baa){ if (baa) foo = "ab"; else foo = "cb";}
strcpy()
void foo(char *foo, int baa){ if (baa) strcpy(foo, "ab"); else strcpy(foo, "cb");}
foo
char x[] = "baa";foo(x, 1);printf("%s\n", x);
x
void foo(char **foo, int baa){ if (baa) *foo = "ab"; else *foo = "cb";}
char *x = "baa";foo(&x, 1);printf("%s\n", x);
慕标5832272
TA贡献1966条经验 获得超4个赞
int
double
#include <stdio.h>void incrementInt(int* in, int inc){ *in += inc;}void incrementDouble(double* in, double inc){ *in += inc;}int main(){ int i = 10; double d = 10.2; printf("i: %d, d: %lf\n", i, d); incrementInt(&i, 20); incrementDouble(&d, 9.7); printf("i: %d, d: %lf\n", i, d);}
i: 10, d: 10.200000 i: 30, d: 19.900000
- 3 回答
- 0 关注
- 718 浏览
添加回答
举报
0/150
提交
取消