X
PrevPrev Go to previous topic
NextNext Go to next topic
Last Post 05 Sep 2017 11:44 AM by  David Starbuck
Properly Dereferencing and Setting values via Pointers
 1 Replies
Sort:
You are not authorized to post a reply.
Author Messages

Kevin Dunphy



New Member


Posts:
New Member


--
05 Sep 2017 05:31 AM
    For example:

    IDL> a = 10
    IDL> b = ptr_new( a, /allocate_heap )
    IDL> *b = 20
    IDL> print, a
    10
    IDL>

    I would expect by dereferencing pointer "b", and setting it to another value, this should change the value of "a", although this is not the case. What could I do to get the desired result?

    David Starbuck



    Basic Member


    Posts:143
    Basic Member


    --
    05 Sep 2017 11:44 AM
    When you create the "b" pointer with the following command:
    b = ptr_new( a, /allocate_heap )

    You are allocating a new area in memory that contains a variable with the value in a. When you change the dereferenced "b" value, you are only changing the values stored at the location of the b pointer. The value of 'a' is stored in a different location. I think this might be closer to what you are trying to do:

    IDL> .f
    IDL> a = ptr_new(10)
    IDL> b = a
    IDL> *b = 20
    IDL> print, *a
    20
    You are not authorized to post a reply.