【发布时间】:2013-12-08 13:32:53
【问题描述】:
如果我有这种函数:str1= NULL 开头
Result dosomething(char* str1, char* str){
str1=malloc(strlen(str2)+1); //followed by NULL check
strcpy(str1, str2);
.
.
return something
然后我在其他一些函数中使用这个函数:
char* output="hello";
char* input=NULL;
result=dosomething(input,output);
由于某种原因,使用“dosomething”函数后输入仍然为 NULL,尽管如果我在 strcpy 之后立即使用 printf,我可以看到 str1 确实更改为“hello”。将它们传递给其他函数时,我在这里对 char* 做错了吗?
【问题讨论】:
-
在 C 中,函数参数是按值传递的。也就是说,它们被复制了。现在重新考虑您希望您的代码做什么,它做什么,并考虑为什么(并搜索重复,因为它们有很多)。
标签: c string function pointers