char*&?

This is a discussion on char*&? within the C++ Programming forums, part of the General Programming Boards category; Sorry if this is dumb. What does char*& mean, in something like void someFunc(char*& buf, ...) I understand char*, but ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21