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

Just starting out? Need help? Post your questions and find answers here.
kingwolf71
Posts: 30
Joined: Wed May 01, 2019 10:14 am

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

Post 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
Last edited by kingwolf71 on Tue Apr 30, 2024 2:57 pm, edited 1 time in total.
User avatar
Peter
Posts: 1197
Joined: Mon Feb 24, 2014 10:17 pm
Location: 127.0.0.1:9080
Contact:

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

Post by Peter »

Take a look at the documentation: Pointers and memory access
kingwolf71
Posts: 30
Joined: Wed May 01, 2019 10:14 am

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

Post 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()
User avatar
Peter
Posts: 1197
Joined: Mon Feb 24, 2014 10:17 pm
Location: 127.0.0.1:9080
Contact:

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

Post by Peter »

I don't know how you could solve this natively in SpiderBasic.

Javascript solution:

Code: Select all

! p_ptr()
kingwolf71
Posts: 30
Joined: Wed May 01, 2019 10:14 am

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

Post by kingwolf71 »

that works! thank you
Post Reply