Thread: char*&?

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    13

    char*&?

    Sorry if this is dumb.

    What does char*& mean, in something like

    void someFunc(char*& buf, ...)

    I understand char*, but what does the ampersand do here?

  2. #2
    Registered User
    Join Date
    Dec 2005
    Location
    Colchester, Essex, United Kingdom.
    Posts
    31
    When passing an address to a function, regardless of the type of variable, the function will not work on a copy of that variable, but the actual variable itself. So when you pass a char*& to a function, you are passing it the actual char pointer, not a copy. This is useful if you want to fill an array with values, for example.
    If it wasn't for C, we'd be using BASI, PASAL and OBOL.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by tomcant View Post
    When passing an address to a function, regardless of the type of variable, the function will not work on a copy of that variable, but the actual variable itself. So when you pass a char*& to a function, you are passing it the actual char pointer, not a copy. This is useful if you want to fill an array with values, for example.
    You do not need to pass a reference to pointer to fill array members...
    But you need it if you want to initialize the pointer in the function with new for example before filling the array... In C in this case you pass pointer to pointer, in C++ - reference to pointer
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed