为什么传递字符串不会改变它 [英] Why passing a string does not change it

查看:92
本文介绍了为什么传递字符串不会改变它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一点问题,当传递一个c风格的字符串(char *)并对字符串进行一些更改时,更改不会粘在原始字符串上,如下例所示:

Hi, I have a little problem, when passing a c-style string (char* ) and making some changes to the string, the changes don't stuck to the original one, As in the following example :

#include <iostream>
#include <string.h>

using namespace std;

void someFunc(char* str)
{
    str = "Welcome";
}

int main()
{
    char* str = "Hello";
    cout << str << endl;
    someFunc(str);
    cout << str << endl;
}

他们两个都打印出Hello,我发现 - 使这个工作 - 我应该返回一个char *,即使是通过发送一个指针,加上我已经使用调试器来确保他们两个都指向同一个具有相同地址的东西,所以出了什么问题?



谢谢,Sam。

Both of them prints "Hello", I have found - to make this works - that I should return a char* even by sending a pointer anyway, plus l have used the debugger to make sure that both of them points to the same thing having the same address, so what's wrong ??

Thanks, Sam.

推荐答案

C / C ++ 函数参数由 value 传递,因此您的函数修改副本 of str

为了更改它,你必须传递它的地址(作为指针或引用),例如

In C/C++ function parameters are passed by value, so your function modifies a copy of str.
In order to change it you have to pass its address (as a pointer or a reference), e.g.
//..
void someFunct(const char * & str)
{
  str = "Welcome";
}

int main()
{
    const char* str = "Hello";
    cout << str << endl;
    someFunc(str);
    cout << str << endl;
}





当然,在如此微不足道的情况下,



Of course, in such a trivial case,

const char * someFuct(){ return "Welcome": }



会好得多(使用 std :: string 会更好)。


因为你实际上并不是更改组成字符串的字符:您正在将指针的副本更改为组成字符串的字符。



当您调用函数并通过指针作为参数,默认情况下C ++传递指针的副本。你可以改变函数中的指针,它不会影响外界的指针。



要改变外界的数据,有一对您可以做的事情。

1)更改指针指向的数据:

Because you aren't actually changing the characters that make up the string: you are changing the copy of the pointer to the characters that make up the string.

When you call a function and pass a pointer as a parameter, by default C++ passes a copy of the pointer. You can change the pointer within the function, and it will not affect the pointer in the outside world.

To change the data in the outside world, there are a couple of things you can do.
1) Change the data the pointer points at:
void someFunc(char* str)
{
    *str = 'X';
}



2)将指针传递给指针:


2) Pass a pointer to a pointer:

void someFunc(char* * pstr)
{
    *pstr = "Welcome";
}



3)传递对指针的引用:


3) Pass a reference to the pointer:

void someFunc(char* & str)
{
    str = "Welcome";
}





我讨厌标记[/ edit]





你的意思是我已经传递了指针str和Hello字符串的副本吗?但是我有一个误解,当我使用调试器时我有发现这两个指针都有相同的地址,这是不是意味着它们都指向同一个东西?



O. ..Kay ...

我们需要谈论指针是什么,不是,以及变量是什么。

所以让我们忽略计算机,谈谈关于汽车而不是。



我们都在同一栋楼里工作,它有一个停车场。所以我们正在回家,并到达你停车的地方。

我们可以指着汽车并通过说那辆车来识别它 - 或者我们可以通过说你的车来识别同一车辆汽车(或我的车在你的情况下)。它们是识别同一车辆的不同方式。

如果我从你那里购买汽车(汽车A)并且你进入你的新车(汽车B)那么我们仍然可以识别旧车指着车A并说那辆车,但你的车现在将识别出一辆不同的车 - 车B。

在编程方面,你的车指针已经改变,但那辆车指针没有。



所以如果我们这样做:



[edit]I HATE MARKDOWN[/edit]


"Do you mean that I have passed a copy of pointer str and "Hello" string as well ?? But I have a misunderstanding, when I used the debugger I have found that both of the two pointers have a the same address, does not that mean that both of them points to the same thing ?"

O...Kay...
We need to talk about what a pointer is, and isn't, and what a variable is.
So let's ignore computers, and talk about cars instead.

We both work in the same building, and it has a car park. So we are heading for home, and reach where you parked your car.
We can point at the car and identify it by saying "that car" - or we can identify the same vehicle by saying "your car" (or "my car" in your case). They are different ways to identify the same vehicle.
If I buy the car ("car A") from you and you come in your your new car ("car B") then we can still identify the old car by pointing at "car A" and saying "that car", but "your car" will now identify a different vehicle - "car B".
In programing terms, the "your car" pointer has changed, but the "that car" pointer has not.

So if we do this:

Car* thatCar = &CarA;
Car* yourCar = &CarA;
printf(thatCar.RegistrationNumber);
printf(yourCar.RegistrationNumber);

我们为每辆车获得相同的号码,表示它是同一辆车。

所以,你卖给我你的车然后得到一个新的:

We get the same number for each vehicle, indicating it's the same car.
So, you sell me your car and get a new one:

Car* thatCar = &CarA;
Car* yourCar = &CarA;
Car* myCar = yourCar;
yourCar = &CarB;
printf(thatCar.RegistrationNumber);
printf(yourCar.RegistrationNumber);



这一次,我们得到两个不同的车辆注册,因为更改你的车不会影响那辆车。



你的someFunc是一样的:字符串Hello的地址传递给函数,但不是传递它的变量的地址 - str是一个局部变量,是抛弃当函数结束时,所以someFunc中的str更改不会导致main中的str更改,因为它们是完全不同的变量!

如果你想影响Main中的str,那么你必须通过一个指向变量的指针(而不是它的内容)或对它的引用。



我知道这很复杂,如果没有它很难解释能看到你的眼睛什么时候釉面,但是......这有意义吗?


This time, we get two different vehicle registrations, because changing yourCar does not affect thatCar.

It's the same with your someFunc: the address of the string "Hello" is passed into the function, but not the address of the variable which holds it - str is a local variable and is "thrown away" when the function ends, so changes to str within someFunc do not result in changes to str in Main because they are completely different variables!
If you want to affect the str in Main, then you have to pass through either a pointer to the variable (rather than it's content) or a reference to it.

I know this is complicated, and it's difficult to explain without being able to see when your eyes glaze, over, but...does that make sense?


这篇关于为什么传递字符串不会改变它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆