Page 1 of 1

How to make and use a pointer to a function?[fixed]

Posted: Mon Apr 29, 2024 2:47 pm
by kingwolf71
I am a sucker for pointers, especially pointers to functions and SB already uses them internally.
But how can I use them myself?

Added an example for those like me
Procedure.s ToUpper(s$)
ProcedureReturn UCase(s$)
EndProcedure

Procedure.s ToLower(s$)
ProcedureReturn LCase(s$)
EndProcedure


dim *ptr(2)
define.s s2, s1 = "Tom Sawyer"

*ptr(0) = @ToUpper()
*ptr(1) = @ToLower()


for i = 1 to 10
a = Random(1, 0)

;How do I call the function?
!v_s2 = ap_ptr.array[v_a](v_s1)
debug str(i) + ":" + s2

next

Re: How to make and use a pointer to a function?

Posted: Mon Apr 29, 2024 3:40 pm
by Peter
Take a look at the documentation: Pointers and memory access

Re: How to make and use a pointer to a function?

Posted: Mon Apr 29, 2024 4:22 pm
by kingwolf71
I did
I need a way to call the functions

Code: Select all

Declare.i   Test()


Procedure   Test()
   debug "Hello World!"
EndProcedure


define   *ptr

*ptr = @Test()

;How do I call the function?
*ptr()

Re: How to make and use a pointer to a function?

Posted: Mon Apr 29, 2024 4:30 pm
by Peter
I don't know how you could solve this natively in SpiderBasic.

Javascript solution:

Code: Select all

! p_ptr()

Re: How to make and use a pointer to a function?

Posted: Tue Apr 30, 2024 6:49 am
by kingwolf71
that works! thank you