Author Topic: The difference between the () and the [] - a nearly undocumented feature of PB  (Read 2210 times)

0 Members and 1 Guest are viewing this topic.

Offline Theo Gottwald

  • Administrator
  • Hero Member
  • *****
  • Posts: 923
  • User-Rate: +30/-4
    • it-berater
Quote
Theo,
You posted an article about "Using Indexed String Pointers" on Jose's board.

Can you tell me the difference between the square brackets @c[3] and a typical subscript a(3)??

Thanks for your time. ray

At least i can try. Run this program below and take a look at the last part and the result of the MSGBOX commands.
This should explain the difference.

Generally () show an Array element of a dimensioned Array.
While the [] add a nuber to a Pointer Target.

Code: [Select]
#COMPILE EXE
#DIM ALL
#REGISTER NONE

FUNCTION PBMAIN

    DIM a(2) AS STRING
    DIM b(6) AS STRING
    DIM c AS STRING POINTER

    a(0) = "a0"
    a(1) = "a1"
    a(2) = "a2"

    b(0) = "b0"
    b(1) = "b1"
    b(2) = "b2"
    b(3) = "b3"
    b(4) = "b4"
    b(5) = "b5"
    b(6) = "b6"

    ' c = a
    c = VARPTR(a(0))
    MSGBOX @c[1]

    ' c = b
    c = VARPTR(b(0))
    MSGBOX @c[3]

   [b] ' Change b
    b(3) = "TEST"
    c = VARPTR(b(2))
    MSGBOX @c[3]
    MSGBOX b(3)

END FUNCTION
          [/b]        

I have found a more complicated example in my codebase:

Code: [Select]
' Reserve free Element, set Strings (nur T03 bis T10) and return index to it, Multithreading-safe
FUNCTION M_AZ(BYVAL T01 AS M_Enum PTR,BYREF T03 AS STRING,BYREF T04 AS STRING,BYREF T05 AS STRING,BYREF T06 AS STRING _
  ,BYREF T07 AS STRING,BYREF T08 AS STRING,BYREF T09 AS STRING,BYREF T10 AS STRING) AS LONG
  REGISTER R01 AS LONG,R02 AS LONG
  IF (@T01.E<3) THEN R02=-1:GOTO enx
  EnterCriticalSection @T01.@CS
  R01=M_AP(T01,4) ' Get/reserve next Free
  R02=@T01.E
   @T01.@SA[0 OF R02,R01 OF @T01.D]=T03
    @T01.@SA[1 OF R02,R01 OF @T01.D]=T04
   @T01.@SA[2 OF R02,R01 OF @T01.D]=T05
    @T01.@SA[3 OF R02,R01 OF @T01.D]=T06
   @T01.@SA[4 OF R02,R01 OF @T01.D]=T07
    @T01.@SA[5 OF R02,R01 OF @T01.D]=T08
   @T01.@SA[6 OF R02,R01 OF @T01.D]=T09
    @T01.@SA[7 OF R02,R01 OF @T01.D]=T10
  LeaveCriticalSection @T01.@CS
  enx:
  FUNCTION=R01
 END FUNCTION     
« Last Edit: January 10, 2011, 07:49:18 AM by Theo Gottwald »