Oxygen Basic

Programming => Problems & Solutions => Topic started by: Brian Alvarez on February 03, 2021, 08:29:05 AM

Title: Question about pointers.
Post by: Brian Alvarez on February 03, 2021, 08:29:05 AM
 In my opinion, the second print statement of the following code should print 101 ("e"), instead it prints 72 ("H").

Code: [Select]
        string ss = "Hello World"
        byte ptr bb = strptr(ss)
        long ii = 1
       
        print bb[ii+1] cr     ' Correctly prints 101
        print bb[(ii+1)] cr   ' IMO, should print 101, but prints 72.
        print bb[(ii)+1] cr   ' Correctly prints 101

 I would guess no inline math is allowed in squared brackets, as i kind of recall reading a year or so ago, but the other additions work fine. What causes the inconsistency?

Title: Re: Question about pointers.
Post by: JRS on February 03, 2021, 12:36:35 PM
I would be interested to know what you get with this statement.

print bb[(ii+1)+0]

Do nothing () may be the issue Charles didn't anticipate.

PRINT ((1+1)) seems like the same direction.
Title: Re: Question about pointers.
Post by: Charles Pegge on February 03, 2021, 02:55:13 PM
The problem was an excessive indexer optimisation. It is quite easy to fix :)
Title: Re: Question about pointers.
Post by: Brian Alvarez on February 03, 2021, 03:30:41 PM

 Thanks Charles. I think you also experience the same things as me... usually removing stuff fixes it.  ;D
Title: Re: Question about pointers.
Post by: JRS on February 06, 2021, 03:45:48 PM
I was curious how ScriptBasic would handle my silly example. Glad to see it's smarter than the programmer.  :)

Code: Script BASIC
  1. PRINT ((1+1)),"\n"


C:\ScriptBASIC\examples>sbc ()test.sb
2

C:\ScriptBASIC\examples>


Title: Re: Question about pointers.
Post by: Brian Alvarez on February 06, 2021, 07:27:29 PM
I was curious how ScriptBasic would handle my silly example. Glad to see it's smarter than the programmer.  :)

Code: Script BASIC
  1. PRINT ((1+1)),"\n"


C:\ScriptBASIC\examples>sbc ()test.sb
2

C:\ScriptBASIC\examples>



 ;D ;D ;D
Title: Re: Question about pointers.
Post by: Nicola on February 07, 2021, 09:25:49 AM
Hello.

Code: OxygenBasic
  1. use console
  2.  
  3. string ss = "Hello World"
  4. byte ptr bb = strptr(ss)
  5. long ii = 1
  6.      
  7. print bb[ii+1] cr     ' Correctly prints 101
  8. print bb[(ii+1)] cr   ' IMO, should print 101, but prints 72.
  9. print bb[(ii+1)+0] cr ' prints 101
  10. print bb[(ii)+1] cr   ' Correctly prints 101
  11.  
  12. waitkey
  13.