Author Topic: passing a chain of CHAR byref  (Read 288 times)

0 Members and 1 Guest are viewing this topic.

Brian Alvarez

  • Full Member
  • ***
  • Posts: 231
    • PluriBASIC
passing a chain of CHAR byref
« 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


Brian Alvarez

  • Full Member
  • ***
  • Posts: 231
    • PluriBASIC
Re: passing a chain of CHAR byref
« Reply #1 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...

Brian Alvarez

  • Full Member
  • ***
  • Posts: 231
    • PluriBASIC
Re: passing a chain of CHAR byref
« Reply #2 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.

Charles Pegge

  • Admin Support Member
  • *****
  • Posts: 3974
    • Oxygen Basic
Re: passing a chain of CHAR byref
« Reply #3 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

Brian Alvarez

  • Full Member
  • ***
  • Posts: 231
    • PluriBASIC
Re: passing a chain of CHAR byref
« Reply #4 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. :)

Charles Pegge

  • Admin Support Member
  • *****
  • Posts: 3974
    • Oxygen Basic
Re: passing a chain of CHAR byref
« Reply #5 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

Brian Alvarez

  • Full Member
  • ***
  • Posts: 231
    • PluriBASIC
Re: passing a chain of CHAR byref
« Reply #6 on: December 10, 2018, 01:54:14 PM »
Perfect, this way i can do it in one line. :)