无法更改传递给函数的字符串。

huangapple go评论76阅读模式
英文:

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&#39;s perspective it has changed, but it has not changed from the program&#39;s perspective.

However if I don&#39;t use `changeStr()` and set the string myself, it seems to work.
```c
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#include &lt;stdlib.h&gt;

/* set string to newStr */
void changeStr(char *string, char *newStr) {
	string = realloc(string, strlen(newStr) + 1);
	memcpy(string, newStr, strlen(newStr) + 1);
	printf(&quot;String from function=%s\n&quot;, string);
}

int main() {
	char *str = NULL;
	changeStr(str, &quot;Hello world!&quot;);
	printf(&quot;String=%s\n&quot;, str);

	/* set string without function */
	char *test = NULL;
	char *newStr = &quot;hello world&quot;;
	test = realloc(test, strlen(newStr) + 1);
	memcpy(test, newStr, strlen(newStr) + 1);
	printf(&quot;NewStr=%s\n&quot;, 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(&quot;String from function=%s\n&quot;, string);
    return string;  // &lt;-- return the updated string object to caller
}

int main() {
    char *str = NULL;
    str = changeStr(str, &quot;Hello world!&quot;);
    printf(&quot;String=%s\n&quot;, str);

huangapple
  • 本文由 发表于 2023年1月9日 15:24:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75054195.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定