Array pointers in PB (+xor example)

Share your advanced PureBasic knowledge/code with the community.
Dr. Dri
Enthusiast
Enthusiast
Posts: 243
Joined: Sat Aug 23, 2003 6:45 pm

Array pointers in PB (+xor example)

Post by Dr. Dri »

Code updated For 5.20+

I don't know if somebody did the same but I think this way to handle pointers is very usefull
  • similar to C syntax
  • clean code (less variables needed)
  • that's all :lol:
small example

Code: Select all

Structure Bytes
  b.b[0]
EndStructure

String.s = "PureBasic"
*p.Bytes = @String

i.l
While *p\b[i]
  Debug Chr( *p\b[i] )
  i + 1
Wend
here is a little sample with a xor function...

Code: Select all


Procedure XorMemory(*Memory.Bytes, MemoryLength.l, *Key.Bytes, KeyLength.l)
  Protected XorMemory.l, i.l, j.l
 
  If *Memory And *Key And MemoryLength > 0 And KeyLength > 0
   
    While i < MemoryLength
      If j = KeyLength : j = 0 : EndIf
      *Memory\b[i] ! *Key\b[j]
      
      ;the two lines above could also be wrote this way :
      ;*Memory\b[i] ! *Key\b[i % KeyLength]
      ;so the j variable becomes useless
      
      i + 1
      j + 1
    Wend
   
    XorMemory = #True
  EndIf
 
  ProcedureReturn XorMemory
EndProcedure

;-test of the procedure
string.s = "a try of the xor width the 'array pointer' trick"
key.s    = "this is the xor key"

length = Len(string) ;it may change after xor

XorMemory(@string, length, @key, Len(key))
Debug string

XorMemory(@string, length, @key, Len(key))
Debug string
Dri :idea: