Programming > Problems & Solutions

Question about pointers.

(1/2) > >>

Brian Alvarez:
 In my opinion, the second print statement of the following code should print 101 ("e"), instead it prints 72 ("H").


--- Code: ---        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
--- End code ---

 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?

JRS:
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:
The problem was an excessive indexer optimisation. It is quite easy to fix :)

Brian Alvarez:

 Thanks Charles. I think you also experience the same things as me... usually removing stuff fixes it.  ;D

JRS:
I was curious how ScriptBasic would handle my silly example. Glad to see it's smarter than the programmer.  :)


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

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

C:\ScriptBASIC\examples>


Navigation

[0] Message Index

[#] Next page

Go to full version