英文:
Can't change string given to function
问题
再次,我遇到了另一个意外情况。
在下面的代码中,似乎如果我使用 `changeStr()` 来改变一个字符串,从函数的角度来看它已经改变了,但从程序的角度来看它并没有改变。
然而,如果我不使用 `changeStr()`,而是自己设置字符串,似乎可以正常工作。
```c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/* 将字符串设为 newStr */
void changeStr(char *string, char *newStr) {
string = realloc(string, strlen(newStr) + 1);
memcpy(string, newStr, strlen(newStr) + 1);
printf("函数中的字符串=%s\n", string);
}
int main() {
char *str = NULL;
changeStr(str, "Hello world!");
printf("字符串=%s\n", str);
/* 在函数外部设置字符串 */
char *test = NULL;
char *newStr = "hello world";
test = realloc(test, strlen(newStr) + 1);
memcpy(test, newStr, strlen(newStr) + 1);
printf("新字符串=%s\n", test);
free(str);
free(test);
return 0;
}
输出:
函数中的字符串=Hello world!
字符串=(null)
新字符串=hello world
我知道在 C 中函数参数会被复制到一个新的变量中,但只有 指针 被复制了 - 而不是 字符串 本身 - 那么这里发生了什么呢?
附言:我并不是一个多年经验的程序员,所以不确定是否有什么问题。
<details>
<summary>英文:</summary>
Once again, I have stumbled across another unexpected occurrence.
In the following code, it seems that if I use `changeStr()` to change a string, from the function's perspective it has changed, but it has not changed from the program's perspective.
However if I don't use `changeStr()` and set the string myself, it seems to work.
```c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/* set string to newStr */
void changeStr(char *string, char *newStr) {
string = realloc(string, strlen(newStr) + 1);
memcpy(string, newStr, strlen(newStr) + 1);
printf("String from function=%s\n", string);
}
int main() {
char *str = NULL;
changeStr(str, "Hello world!");
printf("String=%s\n", str);
/* set string without function */
char *test = NULL;
char *newStr = "hello world";
test = realloc(test, strlen(newStr) + 1);
memcpy(test, newStr, strlen(newStr) + 1);
printf("NewStr=%s\n", test);
free(str);
free(test);
return 0;
}
Output:
String from function=Hello world!
String=(null)
NewStr=hello world
I know that function parameters in C are duplicated into a new variable for the function, but only the pointer is duplicated - not the string itself - so what's going on here?
P.S. I am no multi-year experienced programmer, so not sure if something is wrong.
答案1
得分: 3
realloc()
函数的调用会创建一个新对象并返回指向它的指针,该指针被分配给changeStr()
函数中的string
局部变量。
如果你希望调用函数(main()
)能够访问到新分配的对象,那么被调用函数(changeStr()
)需要将新的指针传递回调用者(例如,作为返回值)。例如:
char *changeStr(char *string, char *newStr) {
string = realloc(string, strlen(newStr) + 1);
memcpy(string, newStr, strlen(newStr) + 1);
printf("String from function=%s\n", string);
return string; // <-- 返回更新后的字符串对象给调用者
}
int main() {
char *str = NULL;
str = changeStr(str, "Hello world!");
printf("String=%s\n", str);
}
英文:
The realloc()
call as you are using it creates a new object and returns the pointer to it, which gets assigned into the string
local variable in changeStr()
.
If you want the caller function (main()
) to have access to that newly allocated object, then the callee function (changeStr()
) needs to pass the new pointer back to the caller (e.g. as a return value). For example:
char *changeStr(char *string, char *newStr) {
string = realloc(string, strlen(newStr) + 1);
memcpy(string, newStr, strlen(newStr) + 1);
printf("String from function=%s\n", string);
return string; // <-- return the updated string object to caller
}
int main() {
char *str = NULL;
str = changeStr(str, "Hello world!");
printf("String=%s\n", str);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论