如何更改传递给C++中函数的字符串指针的值?

发布于 2025-01-17 20:21:39 字数 607 浏览 2 评论 0原文

我需要使用功能更改std ::字符串的值。

该函数必须是无效的,并且参数必须是指向字符串的指针,如图所示。

#include <iostream>

void changeToBanana(std::string *s) {
    std::string strGet = "banana";
    std::string strVal = strGet;
    s = &strVal;
}

    int main() {
    std::cout << "Hello, World!" << std::endl;

    std::string strInit = "apple";
    std::string* strPtr;
    strPtr = &strInit;
        changeToBanana(strPtr);
        std::cout << *strPtr << std::endl;
        return 0;
    }

我希望由此产生的印刷品说“香蕉” 其他答案涉及更改参数。

我尝试使用for循环分配字符串,并按元素按元素进行操作,但这无效。值保持不变。

I need to change the value of a std::string using a function.

The function must be void, and the parameter must be a pointer to a string as shown.

#include <iostream>

void changeToBanana(std::string *s) {
    std::string strGet = "banana";
    std::string strVal = strGet;
    s = &strVal;
}

    int main() {
    std::cout << "Hello, World!" << std::endl;

    std::string strInit = "apple";
    std::string* strPtr;
    strPtr = &strInit;
        changeToBanana(strPtr);
        std::cout << *strPtr << std::endl;
        return 0;
    }

I would like the resulting print to say "banana"
Other answers involve changing parameter.

I have tried assigning the string using a for loop, and going element by element, but that did not work. The value remained the same.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冷心人i 2025-01-24 20:21:39

该函数必须是无效的,并且该参数必须是如图所示的字符串指针。

对于此要求,您无法更改传递给该函数的指针的值,因为它按值传递。

不要将指针与指向的指向混淆。参数按值传递(除非您通过参考将其传递)。进行了副本,并且您对函数中s进行的任何更改都不适用于main中的指针。

但是,您可以更改指针指向的字符串(因为s指向与main中的指针相同的字符串):

void changeToBanana(std::string *s) {
    std::string str = "banana";
    *s = str;
}

但是,这不是惯用的C ++。您应该传递AA参考void changetobanana(std :: string&amp; s)或返回字符串std :: String returnBanana()

The function must be void, and the parameter must be a pointer to a string as shown.

With this requirements you cannot change the value of the pointer that is passed to the function, because it is passed by value.

Don't confuse the pointer with what it points to. Parameters are passed by value (unless you pass them by reference). A copy is made and any changes you make to s in the function do not apply to the pointer in main.

However, you can change the string pointed to by the pointer (because s points to the same string as the pointer in main):

void changeToBanana(std::string *s) {
    std::string str = "banana";
    *s = str;
}

However, this is not idiomatic C++. You should rather pass a a reference void changeToBanana(std::string& s) or return the string std::string returnBanana().

魂牵梦绕锁你心扉 2025-01-24 20:21:39
void changeToBanana(std::string *s) {
    std::string strGet = "banana";
    std::string strVal = strGet;
    
    // the following line doesn't do anything
    //  useful, explanation below
    s = &strVal;
}

s =&amp; strvalstrval的地址分配给s。然后,函数结束和对s进行的任何修改是“忘记”,becase s local changetobanana local 变量>。

因此,调用changetobanana(&amp; foo)什么也不做。

您要么想要:

void changeToBanana(std::string *s) {
    *s = "banana";
}
...
std::string strInit = "apple";
changeToBanana(&strInit);    

要么是这样(因为您不需要指针而选择):

void changeToBanana(std::string & s) {
    s = "banana";
}
...
std::string strInit = "apple";
changeToBanana(strInit);
void changeToBanana(std::string *s) {
    std::string strGet = "banana";
    std::string strVal = strGet;
    
    // the following line doesn't do anything
    //  useful, explanation below
    s = &strVal;
}

s = &strVal assigns the address of the strVal to s. Then the function ends and any modifications made to s are "forgotton", becase s is a local variable of changeToBanana.

So calling changeToBanana(&foo) does nothing.

You either want this:

void changeToBanana(std::string *s) {
    *s = "banana";
}
...
std::string strInit = "apple";
changeToBanana(&strInit);    

or this (preferred because you don't need pointers):

void changeToBanana(std::string & s) {
    s = "banana";
}
...
std::string strInit = "apple";
changeToBanana(strInit);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文