scope of a pointer?

This is a discussion on scope of a pointer? within the C++ Programming forums, part of the General Programming Boards category; If a pointer is passed to a function, does it go out of scope when the function returns? Maybe a ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    203

    scope of a pointer?

    If a pointer is passed to a function, does it go out of scope when the function returns? Maybe a better way to ask would be, I send a pointer of array x to a function. The function does some stuff then increments the pointer. I know that since it's a pointer, any data that the pointer points to will stay changed, but will the pointer stay incremented (pointing to the next location in the array) after said function has ended? If not, then would 2 possible solutions to have it stay incremented be either looking up how to send a pointer pass-by-reference or use the pointer as the return value? Right now i'm sending an int by reference and using it to index the array, but it seems ugly and excessive since i'm sending a pointer already.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    No, the pointer will not stay incremented (just like the int wouldn't stay incremented if you passed it to by value). You can pass a pointer by reference: (int*& p). I don't think you can pass an array pointer by reference, but you shouldn't have to since the pointer syntax works as well.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,625
    The short answer is that the pointer will not stay incremented.

    In this example;
    Code:
    //    include relevant_headers
    
    void function(int *x)
    {
        x[0] = 5;
        ++x;
    }
    
    int main()
    { 
        int arr[5] = {0};        // arr contains 5 zeros
        int *orig_arr = arr;    //   store pointer to array
    
        function(arr);
        assert(orig_arr == arr);
        assert(orig_arr[0] == 5);
       assert(arr[0] == 5);   
    }
    then ALL of the assertions will succeed.

    Essentially, if the workings of a function change any argument that is passed by value to the function, then that change is not visible to the caller. If you want the change to be visible then, yes, you must pass either a pointer or a reference to the thing you want changed. If you want the change to a pointer to be visible, you must either pass a pointer to pointer or a reference to pointer. However, you cannot do that with an array;
    Code:
    // include necessary headers
    
    void function(int **x, int *&y)
    {
        ++(*x);
        ++y;
    }
    
    int main()
    {
            int *x = new int[5];
            int *y = new int[5];
            int *px = x;    // store original pointers
            int *py = y;
    
           function(&x, y);
           assert(x == px + 1);
           assert(y == py + 1);
    
           int arr[5];
           int arr2[5];
           function((int *)&arr, arr2);     // undefined behaviour as arr and arr2 are really arrays not pointers
    
    }

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    If a pointer is passed to a function, does it go out of scope when the function returns?
    Anything that is passed to a function is copied and then assigned to the function parameter variable. Therefore anything you do to the function parameter variable has no effect on the original variable. When the function ends, the function parameter variable is destroyed.

    Inside a function, you can change the thing that is located at the address stored in a pointer. A pointer is an address and when it is copied for a function, the copy of the address still refers to the same location in memory, and you can use the copy of the address to access the original thing that is stored there. However, if you try to assign a different address to a function parameter variable that is a pointer, either by using pointer arithmetic or by using outright assignment, the address stored in the original pointer variable will remain unchanged.
    Last edited by 7stud; 12-29-2005 at 09:06 PM.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    203
    So what's the syntax for passing a pointer by reference (for both the function decleration and call)?

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Look at grumpy's second example. It doesn't work with the locally defined arrays themselves, but if you pass a pointer to the array then it will get updated correctly.

    If that doesn't help you, then you might just have to return the pointer or index from the function.

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    203
    Thanks for the help and quick replies. It's working perfectly

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 05-15-2009, 09:38 AM
  2. What is a virtual function pointer?
    By ting in forum C++ Programming
    Replies: 4
    Last Post: 03-05-2008, 02:36 AM
  3. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 04:36 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 01:04 PM

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