Can this be solved by a pointer, if so how please?

New to FreeBASIC? Post your questions here.
olympic sleeper
Posts: 41
Joined: Jun 07, 2020 15:47

Can this be solved by a pointer, if so how please?

Postby olympic sleeper » Jul 26, 2020 11:10

Hi,

I have a code block that I want to reuse with a slightly different reference, it has a with statement at its start - with db(place). db is a type, and db(place) is an array of this starting at 0.

I also have a pointer *objects which links to db via objects(object), I guess you could think of this as a key for just objects where as db is everything.

I want to reuse the block and replacing the with db(place) with, with *objects(obj) will do this, but I really do not want to duplcate everything. I don't think I can use a simple if (have not tried this yet), though free basic has surprised me in the past.

Code: Select all

if x then with db(place)
else with *objects(obj)


so can I use a pointer and pass the pointer to a sub? If so how would I set the pointer up so that it can point to either db(x) or *objects(x) and how would I pass this to the sub? Finally once inside the sub would with *pointer_name(x) be sufficent or do I need something else?

Thanks in advance.
dodicat
Posts: 6628
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Can this be solved by a pointer, if so how please?

Postby dodicat » Jul 26, 2020 12:33

If you pass a single pointer to a sub then you should also pass the upper limit of the pointer.
With arrays passed to a sub the limits are found using lbound and ubound.

Code: Select all

 

Type db
   As Long n
End Type

Sub show overload(p As db Ptr,n As Long)
   For place As Long=0 To n
      Print p[place].n;
   next place
   Print
End Sub

Sub show(p() As db Ptr)
   For place As Long=LBound(p) To UBound(p)
      Print p(place)->n;
   next place
   Print
End Sub

Sub show(p() As db)
   For place As Long=LBound(p) To UBound(p)
      Print p(place).n;
   next place
   Print
End Sub

Dim As db x(10)
For place As Long=0 To 10
   With x(place)
      .n=place*2
   End With
Next place

show(x())

'method 1
Dim As db Ptr objects(10)
For place As Long=0 To 10
   objects(place)=@x(place)'must point to a location OR allocate new memory
   With *objects(place)
      .n=place*3
   End With
Next place

show(objects())

'method 2
Dim As db Ptr z=@x(0)
For place As Long=0 To 10
    z[place].n=place*4
Next place

show(z,10)
print
show(x())'changed because the above pointers are using the same memory location as x()



Sleep

badidea
Posts: 2104
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Can this be solved by a pointer, if so how please?

Postby badidea » Jul 26, 2020 23:06

olympic sleeper wrote:so can I use a pointer and pass the pointer to a sub?

I may misunderstand your question, but: yes. Instead of pointers, we can also use 'byref' (by reference). Example:

Code: Select all

'just some user defined type / struct
type my_object
   dim as integer a,b,c
end type

sub set_object(byref object_ as my_object) 'note: byref optional here
   with object_
      .a = 99 : .b = 999 : .c = 9999
   end with
end sub

dim as my_object db(0 to 9)
dim as my_object ptr pObject = @db(0)

if false then 'change to true for test
   set_object(db(3))
else
   set_object(*pObject)
end if

print db(3).a
print pObject->a

The above is a bit weird code, this can be re-written as:

Code: Select all

type my_object
   dim as integer a,b,c
   declare sub set()
end type

sub my_object.set()
   a = 99 : b = 999 : c = 9999
end sub

dim as my_object db(0 to 9)
dim as my_object ptr pObject = @db(0)

if true then 'change to true for test
   db(3).set()
else
   pObject->set()
end if

print db(3).a
print pObject->a

Usually, 'don't duplicate everything' means 'use a sub/function'.

Return to “Beginners”

Who is online

Users browsing this forum: Baidu [Spider] and 1 guest