Author Topic: Question about pointers.  (Read 596 times)

0 Members and 1 Guest are viewing this topic.

Brian Alvarez

  • Guest
Question about pointers.
« 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?

« Last Edit: February 03, 2021, 09:46:33 AM by Brian Alvarez »

JRS

  • Guest
Re: Question about pointers.
« Reply #1 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.

Charles Pegge

  • Guest
Re: Question about pointers.
« Reply #2 on: February 03, 2021, 02:55:13 PM »
The problem was an excessive indexer optimisation. It is quite easy to fix :)

Brian Alvarez

  • Guest
Re: Question about pointers.
« Reply #3 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

JRS

  • Guest
Re: Question about pointers.
« Reply #4 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>



Brian Alvarez

  • Guest
Re: Question about pointers.
« Reply #5 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

Nicola

  • Guest
Re: Question about pointers.
« Reply #6 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.