What is the difference between *&array and *array?

This is a discussion on What is the difference between *&array and *array? within the C++ Programming forums, part of the General Programming Boards category; I'm trying to pass in an array into a function. But what is the difference between Code: void function(int *array) ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    163

    What is the difference between *&array and *array?

    I'm trying to pass in an array into a function. But what is the difference between

    Code:
    void function(int *array)
    void function(int *&array)

  2. #2
    1479
    Join Date
    Aug 2003
    Posts
    253
    Code:
    void function(int *arrray)
    This passes a pointer to an array of integers. Usually reflects a numerical or character value...in this case numerical.
    Code:
    void function(int *&array)
    This passes the address reserved in memory of the array.
    Knowledge is power and I want it all

    -0RealityFusion0-

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    163
    so regardless of which i use, it is the same right?

  4. #4
    1479
    Join Date
    Aug 2003
    Posts
    253
    Depends on how you use it. If you are wanting to use a specific element of the array in the function then you would use the int *array. Your best bet would be to try it both ways. If you get some odd value like 0xffff8763f then you are getting the address.
    Knowledge is power and I want it all

    -0RealityFusion0-

  5. #5
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    well, if you want to change where a pointer points, you will want to pass a reference to it...

    Code:
    void myfunc(int *arr)
    {
        arr = NULL;
    }
    
    int *arr = new int[5];
    myfunc(arr);
    
    arr still is valid, not null.
    Code:
    void myfunc(int *&arr)
    {
        arr = NULL;
    }
    
    int arr = new int[5];
    myfunc(arr);
    
    arr is now NULL.

  6. #6
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    so regardless of which i use, it is the same right?
    No, they're not quite the same. When you use arrays, the compiler reserves some amount of space, the beginning of which has the address &A[0](A being an array of course). When you pass an array into a function, your program passes not the array, but the beginning address of the array. The second example you'll use less often. When the need for modifying a pointer in function occurs you either pass in a pointer to a pointer or you pass a reference to a pointer. The syntax isn't that clear, so you might rewrite
    Code:
    typedef Node* NodePtr;
    
    void f(NodePtr& p)]
    {
           p = 0;
    }

  7. #7
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    the first is passing by value. the second is passing by reference.
    its the same as(but with pointers instead)....
    void func (int); // by value
    void func (int&); // by reference

    Theres some more info about this in the FAQ. Look at the pointer tuts.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  8. #8
    Registered User
    Join Date
    Dec 2004
    Posts
    163
    Initially I got confused by a book which states that array is always pass by reference. Now I understand what is going on. Thanks guys for your help!
    rockytriton explanation is pretty good!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Review required for program of date difference
    By chottachatri in forum C Programming
    Replies: 6
    Last Post: 10-31-2008, 12:46 PM
  2. Finding the difference in two times
    By muthus in forum C++ Programming
    Replies: 4
    Last Post: 01-24-2008, 06:36 PM
  3. Difference Equations / Recurrence Relations
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 10-05-2007, 11:26 AM
  4. What's the difference between var++ and ++var
    By ulillillia in forum C Programming
    Replies: 6
    Last Post: 05-31-2007, 03:27 AM
  5. Replies: 10
    Last Post: 11-06-2005, 09:29 PM

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