Oxygen Basic

Programming => Problems & Solutions => Topic started by: Brian Alvarez on December 10, 2018, 10:50:12 AM

Title: passing a chain of CHAR byref
Post by: Brian Alvarez on December 10, 2018, 10:50:12 AM

 Charles, i have tried everything and no solution has been found.
Even if i access the data directly at the address, the string remains the same after exiting the module.
it looks like the CHAR variable is being re-created rather than re-assigned, creating a completely different
address for the variable data.

Can you show me how to do it?

 This is my non-working code:

Code: [Select]
FUNCTION BYREFPAR(byref char s) AS INT
 
   MSGBOX str(@s) & chr(10, 13) & s, , "INSIDE BEFORE"
   
   s = "* changed in module *"
     
   MSGBOX str(@s) & chr(10, 13) & s, , "INSIDE AFTER"
     
END FUNCTION

FUNCTION PBMAIN2() AS INT
   CHAR s[21]
     
   s = "*ORIGINAL STR*" & CHR(0)
   
   MSGBOX str(@s) & chr(10, 13) & s, , "OUTSIDE BEFORE"

   BYREFPAR(s)
       
   MSGBOX str(@s) & chr(10, 13) & s, , "OUTSIDE AFTER"

END FUNCTION

PBMAIN2() ' invoke entry point

Title: Re: passing a chain of CHAR byref
Post by: Brian Alvarez on December 10, 2018, 11:02:10 AM
By the way, couple more things... I also tried:

Code: [Select]
FUNCTION BYREFPAR(char *s) AS INT
Also...
 
Code: [Select]
FUNCTION BYREFPAR(byref char s) AS INT
 
   MSGBOX str(@s) & chr(10, 13) & s, , "INSIDE BEFORE"
   
   s = " hello "            ' this is allowed but does not treat the variable as byref (creates a completely new data address)
   s = ltrim(" hello ")     ' this is not allowed, it doesnt compile.
     
   MSGBOX str(@s) & chr(10, 13) & s, , "INSIDE AFTER"
     
END FUNCTION

 I need to get this working, so i can start converting examples...
Title: Re: passing a chain of CHAR byref
Post by: Brian Alvarez on December 10, 2018, 11:08:56 AM
 By the way, i can make it work by using a low level process, but i want to see if there is already an implemented way to do this.
Title: Re: passing a chain of CHAR byref
Post by: Charles Pegge on December 10, 2018, 11:27:13 AM
Brian,

I think it has to be fairly low level for char*:

Code: [Select]
   's = "* changed in module *"
   copy strptr(s),"* changed in module *",len s
Title: Re: passing a chain of CHAR byref
Post by: Brian Alvarez on December 10, 2018, 12:02:15 PM
Got it!

Thats what i was thinking, so I implemented it that way. Just wanted to know if there was an implemented way already. :)
Title: Re: passing a chain of CHAR byref
Post by: Charles Pegge on December 10, 2018, 01:29:58 PM
Actually, you can simplify it further. Copy understands strings and their pointers:

   copy s,"* changed in module *",len s
Title: Re: passing a chain of CHAR byref
Post by: Brian Alvarez on December 10, 2018, 01:54:14 PM
Perfect, this way i can do it in one line. :)