6

I am reading a post on Stack Overflow and I saw this function:

    advance_buf( const char*& buf, const char* removed_chars, int size );

What does char*& buf mean here and why do people use it?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
brett
  • 5,379
  • 12
  • 43
  • 48

2 Answers2

13

It means buf is a reference to a pointer, so its value can be changed (as well as the value of the area it's pointing to).

I'm rather stale in C, but AFAIK there are no references in C and this code is C++ (note the question was originally tagged ).

For example:

void advance(char*& p, int i) 
{       
    p += i;  // change p
    *p = toupper(*p); // change *p
}

int main() {
    char arr[] = "hello world";
    char* p = arr; // p -> "hello world";
    advance(p, 6);
    // p is now "World"
}

Edit: In the comments @brett asked if you can assign NULL to buff and if so where is the advantage of using a reference over a pointer. I'm putting the answer here for better visibility

You can assign NULL to buff. It isn't an error. What everyone is saying is that if you used char **pBuff then pBuff could be NULL (of type char**) or *pBuff could be NULL (of type char*). When using char*& rBuff then rBuff can still be NULL (of type char*), but there is no entity with type char** which can be NULL.

Motti
  • 110,860
  • 49
  • 189
  • 262
  • why do people use some thing like this ? Is there any advantage of using this ? I think using char** buf also works right ? to provide a clean interface ? – brett Aug 08 '10 at 20:42
  • that, and pointers can point to garbage or to NULL, but references are always valid. – Nathan Fellman Aug 08 '10 at 20:43
  • It is cleanest to code using references than pointers, but under the covers is basically the same char** than char*&. – ggarber Aug 08 '10 at 20:44
  • 1
    @brett, `char**` would also work but it permits NULL while `char*&` does not. Also the syntax is more natural you don't have to add another level of `*`. – Motti Aug 08 '10 at 20:44
  • @Motti what is the disadvantage of allowing assigning to NULL ? Also if I assign buff=NULL is it a syntax error? – brett Aug 08 '10 at 20:50
  • @Brett: you can apply the same argument to *any* reference, not only reference to a pointer ('why use T& when you could use T*?'). Personally I favor T*& (actually I favor to typedef a TPtr type and use TPtr&) because the T** is often used to signify `pointer to head of array of pointers`. – Remus Rusanu Aug 08 '10 at 20:56
  • @brett you can assign `NULL` to `buff` it isn't an error, what everyone is saying is that if you used `char **pBuff` then `pBuff` could be `NULL` (of type `char**`) or `*pBuff` could be `NULL` (of type `char*`). When using `char*& rBuff` then `rBuff` can still be `NULL` (of type `char*`) but there is no entity with type `char**` which can be `NULL`. I hope this makes sense to you. – Motti Aug 09 '10 at 09:02
  • of course, if you used char** const pBuf then you wouldn't be able to set pBuf to NULL either (but you could still set *pBuf to NULL) - so you'll get the same effect as the reference. You will have to dereference pBuf everywhere, though - so the the reference is definitely cleaner. – philsquared Aug 10 '10 at 10:33
4

buf's a (C++) reference to a pointer. You could have a const char *foo in the function calling advance_buf and now advance_buf can change the foo pointer, changes which will also be seen in the calling function.