Author Topic: Request for an example of UDT member pointers  (Read 1836 times)

0 Members and 3 Guests are viewing this topic.

Brian Alvarez

  • Guest
Request for an example of UDT member pointers
« on: November 30, 2018, 01:04:53 AM »

 If somebody can provide an example for this, i would appreciate it.

 I see that TYPE allows PTR members,

Code: [Select]
TYPE SOMETHING
     SOMETHING AS LONG PTR
END TYPE

How can those be used?

Charles Pegge

  • Guest
Re: Request for an example of UDT member pointers
« Reply #1 on: November 30, 2018, 01:59:13 AM »
A simple demo for setting pointered members:

Code: [Select]
type tt
  pf as float ptr
end type

dim t as tt
dim f as float=pi()
@t.pf=@f
print t.pf


Brian Alvarez

  • Guest
Re: Request for an example of UDT member pointers
« Reply #2 on: November 30, 2018, 02:14:57 AM »
I think i understand. I would need to split the assignation into several statements, right?
The way i am trying to emulate is this, suppose i have a variable for an UDT PTR. As follows:

Code: [Select]
type tt
  pf as float ptr
end type

dim t as tt ptr

The way i would do it in PowerBASIC would be to aquire an address for t like this:

Code: [Select]
t = hAddress
And then i would need to aquire an address for pf, as follows:

Code: [Select]
@t.pf = hAddress
Then i would access pf member as follows:

Code: [Select]
@t.@pf = 123
There can even bee deeply nested pointers like this:

Code: [Select]
@t.@pf.@tree.@yikes = 1234
Am i right to assume in Oxygen i need to do this?

Code: [Select]
@t = hAddress1
@t.pf = hAddress2
@t.pf.tree = hAddress3
@t.pf.tree.yikes = hAddress4

And if so... am i right to assume this would return the address of an UDT member 'yikes':

Code: [Select]
print str(@t.pf.tree.yikes)
While this returns the value stored in the member?

Code: [Select]
print str(t.pf.tree.yikes)
« Last Edit: November 30, 2018, 02:26:51 AM by Brian Alvarez »

Charles Pegge

  • Guest
Re: Request for an example of UDT member pointers
« Reply #3 on: November 30, 2018, 03:16:29 AM »
Yes, this is correct.

Code: [Select]
@t = hAddress1
@t.pf = hAddress2
@t.pf.tree = hAddress3
@t.pf.tree.yikes = hAddress4
'
print str(t.pf.tree.yikes)

Fortunately, direct deep pointering is seldom required , though recursive tree stuructures are quite useful.

Code: [Select]
type tt
   pt as tt ptr 'recursive structure
   dat[16] as int
end type

Brian Alvarez

  • Guest
Re: Request for an example of UDT member pointers
« Reply #4 on: November 30, 2018, 03:45:45 PM »

This is a new concept to me charles, how does it work?

Perhaps i can make use of it. :)

Charles Pegge

  • Guest
Re: Request for an example of UDT member pointers
« Reply #5 on: December 03, 2018, 03:52:22 AM »
A structure can refer to itself using a pointer member, without causing any recursion problems.

You can use it as a node for linked lists, tree structures and networks.

Here is a silly example, but it tests the notation:
Code: [Select]
'11:38 03/12/2018
'UDT SELF REFERENCE TEST
type tt
  int a,b,c
  tt* t
end type

tt t
@t.t=@t
t={1,2,3}
print t.b
print t.t.b
print t.t.t.b