The use of &.

This is a discussion on The use of &. within the C++ Programming forums, part of the General Programming Boards category; what does it mean when you place a & after something opposed to before it? Code: &thing thing& I understand ...

  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 04:06 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,980
    You might want to read this FAQ on references.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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 04:29 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,980
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    20,980
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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 09: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

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