-2

I've read many topics about passing pointers to a function by reference, but I couldn't really find an answer. The problem is that after I pass the pointer by reference and change its value, after leaving the function, the value of original pointer doesn't change. I am really stuck with it. Please help me! You're my only hope!

Btw. the code i pasted is the same as the code I need to work on, and I cannot really change the calling of the function. Code:

void f(char *p){
    char *np = new char(100);
    np = "aaaaaaaaaaaaaaaaa";
    p = np;
}

And calling of this function:

void *ptr;
f((char *)&ptr);

I will be very thankful for any help!

Michal
  • 178
  • 10

3 Answers3

4

The problem is that after I pass the pointer by reference and change its value, after leaving the function, the value of original pointer doesn't change.

You aren't passing by reference. This would need to have a function signature like

void f(char *&p){
          // ^

Your code as is only changes a value parameter copy of p:

p = np;    
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • not to mention that there's no pass-by-reference in C. – The Paramagnetic Croissant Jul 22 '15 at 17:52
  • although the initial sentence might be a bit misleading, this answer displays the correct syntax for both C and C++, so why the down vote? – user3629249 Jul 22 '15 at 17:55
  • @TheParamagneticCroissant Removed that part. – πάντα ῥεῖ Jul 22 '15 at 17:56
  • He was "passing a pointer by reference" in the C sense of that phrase (what that phrase would have meant before C++ was invented). That kind of C code can be used in C++, but that terminology for describing the code has been made incorrect (even in C) by the existence of C++. Rather, he is passing a `something**` in disguise as a `char*` and needed to use the disguised `something**` by casting it again. – JSF Jul 22 '15 at 18:49
  • @JSF Yes, and what? That doesn't change the point of using a reference. That passing that address is wrong then, doesn't solve the primary problem, that the address isn't _returned_. – πάντα ῥεῖ Jul 22 '15 at 19:03
2

Your function declaration needs to pass the pointer by reference, like this:

void f(char *&p) {...}
yizzlez
  • 8,757
  • 4
  • 29
  • 44
  • Yeah, sure it works and thank you for answering, but still as i said i cannot really change the way the function takes arguments and how i declare ptr. – Michal Jul 22 '15 at 18:02
  • @Michal then you *can't* pass by reference and your question does not make any sense. Note: this does not require any changes to the caller! – crashmstr Jul 22 '15 at 18:21
1

Your question is unclear, so I need to guess at the question. My guess of the question is you cannot change the signature nor call of the function

void f(char* p);
...
void *ptr;
f((char *)&ptr);

And you want to change the body of f, so it will change ptr to point to the literal within the body of f. If I guessed the question right, the answer would be:

void f(char* p)
{
   *((char**)(p)) = "aaaaaaaaaaaaaaaaa";
}

If I didn't guess the question correctly, please ask more clearly.

In the call you have cast a void** to a char*, dropping a level of indirection, then inside the function you cast that level of indirection back in, and at the same time change the ultimate target from void to char. It isn't all as bizarre as it seems (or I would have guessed differently at your question). Casting things to/from char* may help with aliasing issues (when otherwise the compiler could optimize the whole operation out of existence). So maybe your question is part of something legitimate. But it is ugly enough there must be a better way.

JSF
  • 5,281
  • 1
  • 13
  • 20
  • The "aaaaaaaaaaa" was just an example, inside this function i will get different strings, that i need to get outside this function! But I bielieve You're right! Right not if i am testing outside in locals says following thing: "0x001ecc72 {ConsoleApplication.exe!'string'}"aaaaaaaaaa"". So it seems to work. Thank You! – Michal Jul 22 '15 at 18:30
  • Yes, I knew the "aaaaaaaaaaaaaaaaa" in your question was just a simplification of what you really wanted, and the "aaaaaaaaaaaaaaaaa" in my answer was just a copy from your question. I think you now understand the key portion of the answer, which is casting back to a ** and assigning through that cast. Passing a pointer by reference is a very specific C++ thing very different from what you are doing. So using that phrase and a C++ tag on C question led to answers you did not want. – JSF Jul 22 '15 at 18:38
  • Yes, I do. Thank You again! – Michal Jul 22 '15 at 18:40