如何更改传递给C中的函数的实际参数? [英] How to change the actual argument passed to a function in C?
本文介绍了如何更改传递给C中的函数的实际参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想改变传递给函数,而不是它的一个副本的实际参数。例如:
I want to change the actual argument passed to a function and not a copy of it. For example:
char str[] = "This is a string";
我要打电话到 STR
的值是不同的后创建一个函数。我试图创建一个函数接受的char **
的说法,但我无法得到我想要的东西。
I want to create a function after a call to which the value of str
is different. I tried to create a function accepting char**
as the argument but I just couldn't get what I want.
推荐答案
我想你的意思是这样的:
I think you mean something like this:
void update_string(char ** ptr)
{
*ptr = strdup("This is a test");
return;
}
然后调用这样的功能:
Then call the function like this:
char * str = strdup("hello world\n");
printf("%s\n", str);
update_string(&str);
printf("%s\n", str);
这篇关于如何更改传递给C中的函数的实际参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文