[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Q: pointers inside of an array of structures?



Hi Amara:

I think you are looking for the following:

IDL> thisstruc = {orbit:'', freq:ptr_new()} 

IDL> periodcube = replicate(thisstruc,1)
IDL> periodcube[0].orbit='G2'
IDL> periodcube[0].freq=PTR_NEW(DINDGEN(50))

IDL> tempperiod = thisstruc 
IDL> tempperiod.orbit='G3'
IDL> tempperiod.freq=PTR_NEW(DINDGEN(75))

IDL> newperiodcube=[periodcube,tempperiod]

IDL> HELP, newperiodcube[0], /STR
IDL> HELP, *newperiodcube[0].freq
IDL> HELP, newperiodcube[1], /STR
IDL> HELP, *newperiodcube[1].freq

which outputs:

** Structure <13585f8>, 2 tags, length=12, refs=5:
   ORBIT           STRING    'G2'
   FREQ            POINTER   <PtrHeapVar10>
<PtrHeapVar10>  DOUBLE    = Array[50]
** Structure <13585f8>, 2 tags, length=12, refs=5:
   ORBIT           STRING    'G3'
   FREQ            POINTER   <PtrHeapVar11>
<PtrHeapVar11>  DOUBLE    = Array[75]

Cheers Theo

Amara Graps wrote:
> 
> Hi Folks,
> 
> I am trying to create and build a flexible data structure
> in IDL for a situation where we have fields of different types
> in a structure, and then that structure is made into an array
> of structures using "replicate". And I extend the
> array of structures by concantenating, because I don't
> know how many structures I need.
> 
> My question: Can a field inside of the array-of-structures be
> a pointer ?
> 
> For example --
> 
> I know how to flexibly extend a structure,
> with each of the fields defined the same way:
> 
> -------
> Create an array of an anonymous structure
> IDL> thisstruc = {orbit:'', len:0,freq:DBLARR(100)}
> IDL> periodcube = replicate(thisstruc,1)
> 
> ;Put values into the structure
> IDL> periodcube(0).orbit='G2'
> IDL> periodcube(0).len=50    ;to store length of array : freq
> IDL> periodcube(0).freq=DINDGEN(50)
> 
> ;Create a temporary structure like the original
> IDL> tempperiod = thisstruc
> 
> ;Put values into the temporary structure
> IDL> tempperiod.orbit='G3'
> IDL> tempperiod.len=75
> IDL> tempperiod.freq=DINDGEN(75)
> 
> ;Now extend it by concatenating
> IDL> newperiodcube=[periodcube,tempperiod]
> 
> ;Take a look
> IDL> help, newperiodcube, /st
> ** Structure <89af0>, 3 tags, length=816, refs=4:
>    ORBIT           STRING    'G2'
>    LEN             INT             50
>    FREQ            DOUBLE    Array[100]
> IDL> print, newperiodcube(0).orbit, ' ', newperiodcube(1).orbit
> G2 G3
> IDL> print, newperiodcube(0).len, ' ', newperiodcube(1).len
>       50       75
> -------
> 
> But what about varying data lengths inside of the array of
> structures, for example, if my freq arrays are each a different
> length:
> 
> IDL> thisstruc = {orbit:'', freq:ptr_new()}
> IDL> periodcube = replicate(thisstruc,1)
> 
> ;Put values into the structure
> IDL> periodcube(0).orbit='G2'
> IDL> *periodcube(0).freq=DINDGEN(50)
> % Unable to dereference NULL pointer: <POINTER  (<NullPointer>)>.
> % Execution halted at:  $MAIN$
> 
> What am I doing wrong?
> 
> Is it possible to have pointers inside of an array of structures?
> 
> My pointer programming experiences were 15 yrs ago in a Pascal
> class.. it's quite possible I'm missing something big, with how
> the pointers work...
> 
> Thanks very much in advance,
> 
> Amara
> 
> P.S. IDL Version 5.3 (sunos sparc)
> --
> 
> ***************************************************************
> Amara Graps               | Max-Planck-Institut fuer Kernphysik
> Interplanetary Dust Group | Saupfercheckweg 1
> +49-6221-516-543          | 69117 Heidelberg, GERMANY
>                           * http://galileo.mpi-hd.mpg.de/~graps
> ***************************************************************
>       "Never fight an inanimate object." - P. J. O'Rourke

-- 
----------------------------------------------
Theo Brauers
Institut fuer Atmosphaerische Chemie (ICG-3)
Forschungszentrum Juelich
52425 JUELICH, Germany
Tel. +49-2461-61-6646    Fax. +49-2461-61-5346
http://www.kfa-juelich.de/icg/icg3/MITARBEITER/th.brauers.html