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

0 Members and 1 Guest are viewing this topic.

Brian Alvarez

  • Hero Member
  • *****
  • Posts: 699
    • PluriBASIC
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 »

John

  • Hero Member
  • *****
  • Posts: 3966
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

  • Admin Support Member
  • *****
  • Posts: 4486
    • Oxygen Basic
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

  • Hero Member
  • *****
  • Posts: 699
    • PluriBASIC
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

John

  • Hero Member
  • *****
  • Posts: 3966
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

  • Hero Member
  • *****
  • Posts: 699
    • PluriBASIC
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

  • Jr. Member
  • **
  • Posts: 73
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.