0

I have a function which looks like:

void myFunc(char* myString, char* const buf, int startPos){
    myString = &buf[startPos];
    std::cout << myString << std::endl;     //This outputs fine
}

. . . .

char* myString = 0;
.
.
myFunc(myString, buf, startPos);
std::cout << myString << std::endl;         //This doesnt output anything

Why doesn't printing out the string work after I have made the function call?

user997112
  • 25,084
  • 34
  • 143
  • 278
  • `myString` is a local variable in `myFunc`. The caller won't see any modifications to it. Pass it by reference. – juanchopanza Jul 11 '14 at 09:14
  • possible duplicate of [pass by reference and value with pointers](http://stackoverflow.com/questions/4776010/pass-by-reference-and-value-with-pointers) – John3136 Jul 11 '14 at 09:15
  • @juanchopanza I thought passing a pointer achieved the same result as passing by reference? Otherwise what is the point of passing a pointer, ever? – user997112 Jul 11 '14 at 09:16
  • 3
    Pass a pointer to modify **what it points to**, not to modify the pointer itself. – SirDarius Jul 11 '14 at 09:16
  • 1
    The problem is you'd need to pass a pointer to the pointer to modify the pointer itself, rather than what it points to :) – Luaan Jul 11 '14 at 09:19
  • I looked at the "possible duplicate" link and that recommended char &* (because they gave an example of using int &*) but the compiler complains I cannot do that.... – user997112 Jul 11 '14 at 09:21
  • Is it possible to do this using a reference? I'd like to copy as little as possible. – user997112 Jul 11 '14 at 09:23

3 Answers3

3

When you call

myFunc(myString, buf, startPos);  

myString is copied to the function parameter. Any changes to the pointer myString does not change the pointer myString in main.

Either use char **mystring in function parameter or pass myString by reference.

void myFunc(char&* myString, char* const buf, int startPos){...}
haccks
  • 97,141
  • 23
  • 153
  • 244
  • How would passing myString by reference work? I didn't think it would be char& because that would just be one byte? – user997112 Jul 11 '14 at 09:35
  • @user997112; Updated the answer. See this [answer](http://stackoverflow.com/a/24388287/2455888) too. – haccks Jul 11 '14 at 09:41
0

Why not make the function return the value of mystring?

char* myFunc(char* myString, char* const buf, int startPos){
    myString = &buf[startPos];
    std::cout << myString << std::endl;     //This outputs fine
    return myString;
}

Then print the value:

std::cout << myFunc(myString, buf, startPos) << std::endl;

Or, you could do:

myString = myFunc(myString, buf, startPos);
std::cout << myString << std::endl;
Easton
  • 205
  • 1
  • 5
-1

If you want to modify something in a function, you have to pass a pointer to that "thing"... in this case, your "thing" is a "pointer-to-char" so you need to pass in a "pointer-to-'pointer-to-char'", so:

void myFunc(char** myString, char* const buf, int startPos){
    *myString = &buf[startPos];
    std::cout << *myString << std::endl;     //This outputs fine
}

and call it with:

myFunc(&myString, buf, startPos);

The &myString takes the address of your "pointer-to-char" which will allow the function to modify it; inside the function, you need the extra *s to dereference this address and get to the value you want to change.

TripeHound
  • 2,169
  • 17
  • 30