Page 1 of 1

Can Structures Have Pointers?

Posted: Mon Jun 20, 2005 2:20 am
by Dreglor
Is there a way to have a structure field point to another structure so it can access the fields in that structure so the stucture can have dynamic fields

when I tried it didn't compile :(

Posted: Mon Jun 20, 2005 2:47 am
by jack
there many examples scattered about, try searching for structure+pointer
here's one example.
Pupil wrote:use a pointer and use a dummy array to access it.

Code: Select all

structure testtype
  a.l
  b.s
  parray.l ; array pointer
endstructure

structure dummytype
  value.l[0]
endstructure

a.testtype\parray = GlobalAlloc_(#GMEM_FIXED|#GMEM_ZEROINIT, 1024*4)

*b.dummytype = a\parray

for i = 0 to 10
 *b\value[i] = i
  debug *b\value[i]
next

globalfree_(a\parray)
You have to check so you don't go out of bounds or you will surely have a crash.. I guess you could just as easily use the AllocateMemory() command, but then you have to keep track of the memory bank number also...

Posted: Mon Jun 20, 2005 3:49 am
by Dare2
These two point at each other. :)

Code: Select all

Structure myFirstStructure
  PointsToB.l
  itemA.l
EndStructure

Structure myOtherStructure
  addressOfA.l
  thingB.l
EndStructure

thisIsFirst.myFirstStructure
anotherOne.myOtherStructure

thisIsFirst\itemA=1
thisIsFirst\PointsToB=@anotherOne

anotherOne\thingB=2
anotherOne\addressOfA=@thisIsFirst

*pA.myFirstStructure=anotherOne\addressOfA
Debug *pA\itemA
Debug thisIsFirst\itemA

*pB.myOtherStructure=thisIsFirst\PointsToB
Debug *pB\thingB
Debug anotherOne\thingB

Posted: Mon Jun 20, 2005 6:11 pm
by Pupil
Is this what you're looking for?

Code: Select all

Structure struct1
  StructureUnion
    b.b
    w.w
    l.l
  EndStructureUnion
EndStructure

Structure struct2
  *a.struct1
EndStructure

b.struct1\l = $10203040
a.struct2\a = @b

Debug "$"+Hex(a\a\b)
Debug "$"+Hex(a\a\w)
Debug "$"+Hex(a\a\l)