152

As far as I can tell, there's no reason I shouldn't be allowed to pass a reference to a pointer in C++. However, my attempts to do so are failing, and I have no idea why.

This is what I'm doing:

void myfunc(string*& val)
{
    // Do stuff to the string pointer
}

// sometime later 
{
    // ...
    string s;
    myfunc(&s);
    // ...
}

And I'm getting this error:

cannot convert parameter 1 from 'std::string *' to 'std::string *&'

10 Answers 10

146

Your function expects a reference to an actual string pointer in the calling scope, not an anonymous string pointer. Thus:

string s;
string* _s = &s;
myfunc(_s);

should compile just fine.

However, this is only useful if you intend to modify the pointer you pass to the function. If you intend to modify the string itself you should use a reference to the string as Sake suggested. With that in mind it should be more obvious why the compiler complains about you original code. In your code the pointer is created 'on the fly', modifying that pointer would have no consequence and that is not what is intended. The idea of a reference (vs. a pointer) is that a reference always points to an actual object.

0
97

The problem is that you're trying to bind a temporary to the reference, which C++ doesn't allow unless the reference is const.

So you can do one of either the following:

void myfunc(string*& val)
{
    // Do stuff to the string pointer
}


void myfunc2(string* const& val)
{
    // Do stuff to the string pointer
}

int main()
// sometime later 
{
    // ...
    string s;
    string* ps = &s;

    myfunc( ps);   // OK because ps is not a temporary
    myfunc2( &s);  // OK because the parameter is a const&
    // ...

    return 0;
}
6
  • It’s especially instructive to write the type out the way Mr. Burr does in example No.2 – string* const& val instead of a less-legible equivalent like e.g. const string* &val. To my eyes this is clearly a const reference, to a pointer, to a string. I personally prefer writing T const& instead of const T& in declarations to obtain this exact clarification.
    – fish2000
    Jun 2, 2016 at 14:50
  • 1
    Nit pick (not a criticism): To me, the phrase bind a temporary to the reference is clearer in this answer than @Chris' as reference to an actual string pointer in the calling scope, not an anonymous string pointer. Regardless, both are likely correct from my perspective.
    – kevinarpe
    Jul 30, 2016 at 14:01
  • 1
    @fish2000 Not only that, const string*& and string* const& are actually different types. The first is a non-const reference to a const string*, while the second is a const reference to a string*. Nov 14, 2016 at 17:57
  • @fish2000 beware of "prefering" T const& to const T& - as @Justin Time points out, they are different types. It may have no consequences sometimes, but in other situations it will be absolutely critical to prefer one or other, much like preferring int to double.
    – omatai
    Nov 23, 2016 at 21:55
  • 3
    @fish2000 - one says "here's a reference that you can change to something that you can't change" while the other says "here's a reference you can't change to something you can change". In this example given, you simply can't interchange the two.
    – omatai
    Dec 6, 2016 at 4:29
13

Change it to:

  std::string s;
  std::string* pS = &s;
  myfunc(pS);

EDIT:

This is called ref-to-pointer and you cannot pass temporary address as a reference to function. ( unless it is const reference).

Though, I have shown std::string* pS = &s; (pointer to a local variable), its typical usage would be : when you want the callee to change the pointer itself, not the object to which it points. For example, a function that allocates memory and assigns the address of the memory block it allocated to its argument must take a reference to a pointer, or a pointer to pointer:

void myfunc(string*& val)
{
//val is valid even after function call
   val = new std::string("Test");

}
6

&s produces temporary pointer to string and you can't make reference to temporary object.

1
  • 4
    This is not quite true - you can make a reference to const temporary May 5, 2009 at 8:26
4

Try:

void myfunc(string& val)
{
    // Do stuff to the string pointer
}

// sometime later 
{
    // ...
    string s;
    myfunc(s);
    // ...
}

or

void myfunc(string* val)
{
    // Do stuff to the string pointer
}

// sometime later 
{
    // ...
    string s;
    myfunc(&s);
    // ...
}
3
  • 1
    For what I'm doing though, I need the address of the pointer as well as the pointer itself. I don't want to pass the pointer by value.
    – Alex
    May 5, 2009 at 5:12
  • Still not understand what you try to accomplish. You cannot have "address of the pointer" of "string s", simply because "string s" is not a pointer.
    – Sake
    May 5, 2009 at 5:16
  • @Alex: I take it that you need detect whether the string is exactly the same as another one you are storing and not just whether their contents are the same. If that is the case, note that you can use the address-of operator to a reference and it will get you the address of the referenced object: void f( std::string const & s ) { std::string const *p = &s; } May 5, 2009 at 6:14
3

EDIT: I experimented some, and discovered thing are a bit subtler than I thought. Here's what I now think is an accurate answer.

&s is not an lvalue so you cannot create a reference to it unless the type of the reference is reference to const. So for example, you cannot do

string * &r = &s;

but you can do

string * const &r = &s;

If you put a similar declaration in the function header, it will work.

void myfunc(string * const &a) { ... }

There is another issue, namely, temporaries. The rule is that you can get a reference to a temporary only if it is const. So in this case one might argue that &s is a temporary, and so must be declared const in the function prototype. From a practical point of view it makes no difference in this case. (It's either an rvalue or a temporary. Either way, the same rule applies.) However, strictly speaking, I think it is not a temporary but an rvalue. I wonder if there is a way to distinguish between the two. (Perhaps it is simply defined that all temporaries are rvalues, and all non-lvalues are temporaries. I'm not an expert on the standard.)

That being said, your problem is probably at a higher level. Why do you want a reference to the address of s? If you want a reference to a pointer to s, you need to define a pointer as in

string *p = &s;
myfunc(p);

If you want a reference to s or a pointer to s, do the straightforward thing.

3

Welcome to C++11 and rvalue references:

#include <cassert>
#include <string>

using std::string;

void myfunc(string*&& val)
{
    assert(&val);
    assert(val);
    assert(val->c_str());
    // Do stuff to the string pointer
}

// sometime later 
int main () {
    // ...
    string s;
    myfunc(&s);
    // ...
}

Now you have access to the value of the pointer (referred to by val), which is the address of the string.

You can modify the pointer, and no one will care. That is one aspect of what an rvalue is in the first place.

Be careful: The value of the pointer is only valid until myfunc() returns. At last, its a temporary.

1

I have just made use of a reference to a pointer to make all the pointers in a deleted binary tree except the root safe. To make the pointer safe we just have to set it to 0. I could not make the function that deletes the tree (keeping only the root) to accept a ref to a pointer since I am using the root (this pointer) as the first input to traverse left and right.

void BinTree::safe_tree(BinTree * &vertex ) {
    if ( vertex!=0 ) {  // base case
        safe_tree(vertex->left);    // left subtree.
            safe_tree(vertex->right);   //  right subtree.
          // delete vertex;  // using this delete causes an error, since they were deleted on the fly using inorder_LVR. If inorder_LVR does not perform delete to the nodes, then, use delete vertex;
        vertex=0;  // making a safe pointer
    }
} // end in

Bottom line, a reference to a pointer is invalid when the formal parameter is the (this) pointer.

-3

I know that it's posible to pass references of pointers, I did it last week, but I can't remember what the syntax was, as your code looks correct to my brain right now. However another option is to use pointers of pointers:

Myfunc(String** s)
-10

myfunc("string*& val") this itself doesn't make any sense. "string*& val" implies "string val",* and & cancels each other. Finally one can not pas string variable to a function("string val"). Only basic data types can be passed to a function, for other data types need to pass as pointer or reference. You can have either string& val or string* val to a function.

1
  • 5
    Wrong on all levels. Please refresh your var declaration syntax.
    – Ari
    May 5, 2009 at 8:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.