Back Home Next

Subchapters:

Pointers and Heap Variables


Pointers are defined by the function ptr_new. You can give an initial value to the newly created heap_variable on which the pointer will point, here 50.:

        IDL> p_1 = ptr_new(50.)

If you don't give value, the pointer will point to the special null_value, used for example to specify the end of a tree:

        IDL> p_2 = ptr_new()
        IDL> print,p_2
              <NullPointer>

You can see the difference between the pointer and the heap_variable.
The value of the heap_variable is accessible by putting a star (*) before the name of the pointer:

        IDL> print,p_1
              <PtrHeapVar1>
        IDL> print,*p_1
                  50.0000

The <PtrHeapVar1> is the value of the pointer, and 50.0000 is the value of the heap_variable on which it points.

You can define pointers everywhere, also into structures.  Here follows an example of defining a structure str1 containing, as a second tag, a pointer equal to the already defined p_1 pointer (that is, pointing on the same heap_variable).

You can access the value of the heap_variable in the same way as before, just putting a * in front of the name.

        IDL> str1 = {name:'name',p_3:p_1}
        IDL> print,str1
              { name<PtrHeapVar1>}
        IDL> print,str1.p_3
              <PtrHeapVar1>
        IDL> print,*str1.p_3
                   50.0000

You can modify the value of the heap_variable, and of course all the pointers pointing on this variable will take this modification into account (The heap_variable is defined once, but you can define a lot of pointers pointing on it).

        IDL> *p_1 = 20
        IDL> print,*p_1
                    20
        IDL> print,*str1.p_3
                    20
        IDL> *str1.p_3 = 30
        IDL> print,*p_1
                   30

You can also define a pointer pointing on a structure, then you have to take care of the *s and brackets:

        IDL> p_str1 = ptr_new(str1)
        IDL> print,p_str1
              <PtrHeapVar2>
        IDL> print,*p_str1
              { name<PtrHeapVar1>}
        IDL> print,(*p_str1).p_3
              <PtrHeapVar1>
        IDL> print,*(*p_str1).p_3
                    30

There are other functions to play with pointers, have a look at the man pages.

The pointers and the structures are an (best) alternative solution to common blocks. You just have to define a structure containing all the parameters you need in form of pointers, and pass it, as parameter, to the procedure. Contrary to the common blocks, where you cannot put the same variable into 2 or more commons, with the pointers, you can define a lot of structures corresponding to the set of variables you need for specific applications, and containing pointers to the same variables.

The pointers allow you to change dynamically the value or even the type/size of the variable on which the pointer is pointing.

See the David Fanning examples on widgets. Address in the reference page. 

 

IDL courses C. Morisset © 2004 IA/UNAM V 2.2

Back Home Next