Thread: The use of &.

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

    The use of &.

    what does it mean when you place a & after something opposed to before it?


    Code:
    &thing
    thing&
    I understand the one first one would give me the address of the thing...and the 2nd line's form is used when i want to accept something as a reference (??)

    --
    what is the difference between
    (assuming the & one is even valid..i mean is it?)
    Code:
    int func(int *p)
    Code:
    int func2(int p&)
    ?
    Last edited by rodrigorules; 11-21-2009 at 05:06 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You might want to read this FAQ on references.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    read the article,
    hmm so is placing the ampersand AFTER incorrect syntax?

    I am reading a book and they talk about methods such as
    void process_array( IntArray& );
    void process_array( IntArrayRC& );

    (c++, primer) not sure why the author would write it like this.
    Last edited by rodrigorules; 11-21-2009 at 05:29 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by rodrigorules
    hmm so is placing the ampersand AFTER incorrect syntax?
    In your case, yes, in your book's case, no

    Identify the type and the variable. For reference declarations, the ampersand comes after the type name and before the variable name, if it is present.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Sep 2009
    Posts
    68
    Its important to note that Arrays are always passed by reference therefore it is not necessary to use the ampersand when passing an array through a function

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by bleuz
    Its important to note that Arrays are always passed by reference therefore it is not necessary to use the ampersand when passing an array through a function
    It is not true that arrays are always passed by reference. Rather, they are converted to pointers to their first elements, hence one can modify the contents of an array without passing it by reference.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    The larch
    Join Date
    May 2006
    Posts
    3,573
    To pass an array by reference looks like this:

    Code:
    #include <iostream>
    
    void foo(const int (&arr_ref)[10]) //by reference
    {
        std::cout << sizeof(arr_ref) << '\n'; //size of the array: int[10]
    }
    
    void bar(const int arr_ptr[10]) //usual way, array decays to pointer (10 is ignored)
    {
        std::cout << sizeof(arr_ptr) << '\n'; //size of the pointer: int*
    }
    
    
    int main()
    {
        int arr[10];
        foo(arr);
        bar(arr);
    }
    Note that no decay to pointer occurs and sizeof still reports the size of the array (10 * sizeof(int)).
    Last edited by anon; 11-21-2009 at 10:09 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed