Trick - pointers to arrays

Share your advanced PureBasic knowledge/code with the community.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Trick - pointers to arrays

Post by BackupUser »

Restored from previous forum. Originally posted by tinman.

OK, I searched the forums for this, but coulnd't find it, so apologies if it has been posted somewhere. I was "insprired" by a message to the mailing list where a guy was asking to have PB arrays improved to be more like arrays in C (hmm, or was that the Blitz mailing list?...)

Anyway, you can create a newtype with an array of size zero, allowing you to create pointers to non-specific sized arrays.

Example code:

Code: Select all

; ----8<----8<----8<----
Structure fakearray
    word.w[0]
EndStructure

Dim array.w(10)
For i.w=0 To 9
  array(i)=i*5
Next

*foo.fakearray = @array(0)

If OpenConsole()
  For i=0 To 9
    Print("Item ") : Print(Str(i)) : Print(", contents ") : PrintN(Str(*foo\word[i]))
  Next
  Input()
  CloseConsole()
EndIf
End
; ----8<----8<----8<----

--
It's not minimalist - I'm increasing efficiency by reducing input effort.