Click here to Skip to main content
15,997,960 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 :
C++
#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;
}
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.
Posted

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:
C++
void someFunc(char* str)
{
    *str = 'X';
}

2) Pass a pointer to a pointer:
C++
void someFunc(char* * pstr)
{
    *pstr = "Welcome";
}

3) Pass a reference to the pointer:
C#
void someFunc(char* & str)
{
    str = "Welcome";
}


[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:
C++
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:
C++
Car* thatCar = &CarA;
Car* yourCar = &CarA;
Car* myCar = yourCar;
yourCar = &CarB;
printf(thatCar.RegistrationNumber);
printf(yourCar.RegistrationNumber);

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?
 
Share this answer
 
v3
Comments
CPallini 3-Aug-15 4:21am    
You missed a couple of '*'.
OriginalGriff 3-Aug-15 4:29am    
Nope: Markdown ate the damn things, even though I have it disabled, and it was within a code block...:sigh:
Samuel Shokry 4-Aug-15 23:35pm    
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 ?
Samuel Shokry 5-Aug-15 23:43pm    
I think I have got it but why function like strcpy doesn't work and cause a runtime error ??
OriginalGriff 6-Aug-15 6:38am    
:laugh:
Because you are trying to change the value of a constant!

"Hello" is a constant - it can't change (technically, it's a const char[], or a const char * ) any more than you can change the value of 6 to 77 by doing this:

void myFunc(int* i) { *i = 77; }
...
myFunc(&6);

You would expect that to fail, because otherwise this:
printf("%s\n", 6);
myFunc(&6);
printf("%s\n", 6);

should output
6
77
which it clearly shouldn't! :laugh:
If you want to use strcpy, you need to load the string "Hello" into a separate array of characters and use strcpy on that - but make sure the array you allocate is long enough, or your app will crash!
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.
C++
//..
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,
C++
const char * someFuct(){ return "Welcome": }

would have been far better (and using std::string would have been even better).
 
Share this answer
 
v2
Comments
[no name] 3-Aug-15 4:13am    
Yes and also make sure there is enough memory allocated for new string.
CPallini 3-Aug-15 4:18am    
You don't need to allocate memory for statically allocated strings.
[no name] 3-Aug-15 4:24am    
Ah yes. wasn't concentrating. We are just pointing str to a different literal.
Maciej Los 5-Aug-15 2:48am    
5ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900