Can Structures Have Pointers?

Everything else that doesn't fall into one of the other PB categories.
Dreglor
Enthusiast
Enthusiast
Posts: 759
Joined: Sat Aug 02, 2003 11:22 pm
Location: OR, USA

Can Structures Have Pointers?

Post 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 :(
~Dreglor
jack
Addict
Addict
Posts: 1358
Joined: Fri Apr 25, 2003 11:10 pm

Post 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...
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post 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
@}--`--,-- A rose by any other name ..
Pupil
Enthusiast
Enthusiast
Posts: 715
Joined: Fri Apr 25, 2003 3:56 pm

Post 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)
Post Reply