Page 1 of 1

Pointer to variable

Posted: Sun Oct 30, 2016 11:32 am
by MrTAToad
Having problems with the values modified in variables that are passed as pointers to functions :

Code: Select all

EnableExplicit

Declare.b callback(*one)

Define one

one=9876
Debug "Before : "+Str(one)
callback(@one)
Debug "After : "+Str(one)

  Procedure.b callback(*one)
    *one=1234
    Debug "Changed to : "+Str(*one)
  EndProcedure
For some reason one is always 9876, even though a pointer to one is passed to be modified.

The output is thus :

Before : 9876
Changed to : 1234
After : 9876

Re: Pointer to variable

Posted: Sun Oct 30, 2016 12:36 pm
by Sirius-2337
According to Fred this is a limitation of SB (http://forums.spiderbasic.com/viewtopic ... rt=2#p1725)


You could write your code like this:

Code: Select all

EnableExplicit

Declare.b callback(*one.Integer)

Define one.Integer

one\i=9876
Debug "Before : "+Str(one\i)
callback(@one)
Debug "After : "+Str(one\i)

Procedure.b callback(*one.Integer)
  *one\i=1234
  Debug "Changed to : "+Str(*one\i)
EndProcedure

Re: Pointer to variable

Posted: Sun Oct 30, 2016 2:22 pm
by MrTAToad
Ah, right - thanks for that.