Using the following code:
[syntax="qbasic"]
Declare Sub TestSub (Array() As Integer PTR)
Dim myArray(10) As Integer
Dim myPointer As Integer PTR
myPointer = @myArray
TestSub myPointer
[/syntax]
(May be buggy I know)
First of all, is the sub TestSub asking for an array of pointers? Or a pointer to an array? Second, how would I access (using myPointer) the value at myArray(5)?[/code]
it's asking for an array of pointers, try byref if you just want the array by reference
Code:
Declare Sub TestSub( array() As Integer )
Dim myArray(10) As Integer
TestSub myArray()
Pretty easy, huh? That's how it was done in QB as well.
Jofers, won't that basically make a copy of the array? See, what I'm trying to do is have a sub that will add something to an array specified. Dumbledore, how do I pass by reference?
It's not how you pass it, it's just how you define the sub :-).
Code:
SUB TestSub(ByRef Variable AS INTEGER)
' Add element to your array here
END SUB
If you don't put ByRef like this, it will assume a ByVal parameter passing scheme.
So I'd do something like this:
[syntax="qbasic"]
Sub TestSub(ByRef array() As Integer, number As Integer)
Redim Preserve Array(uBound(Array) + 1)
Array(uBound(Array)) = number
End Sub
[/syntax]
No. Nonono.
There are two ways to pass primitive types (integers, singles, doubles, etc): Byref and Byval. Byref works by passing pointers, so you can pass UDTs (Type structures) this way as well.
However, strings and arrays organize their data with "descriptors", that is, UDTs containing information about the string/array, plus a pointer to the actual data. This is what gets passed when you say:
TestSub array()
...So Byref array() is not allowed because there's more information than what's contained in the data's pointer. This is also why you need to use something like strptr() to get a string's pointer. As proof, here's how you get a string's pointer and length without using strptr or len:
Code:
Type FBSTRING
Data as Byte Pointer
Len As Integer
Size As Integer
End Type
Dim Bob as String
Dim BobDesc as FBSTRING Pointer
Bob = "Hello, my name is bob"
BobDesc = @Bob
Print BobDesc->Data
Print Strptr(Bob)
Print BobDesc->Len
Print Len(Bob)
So what gets passed to the sub is actually a type structure. You can write routines handling FB arrays in other languages by having them pass this type structure:
Code:
Type FBArrayDim
Elements As Integer
LBound As Integer
UBound As Integer
End Type
Type FBArray
Data As Any Pointer
Ptr As Any Pointer
Size As Integer
Element_Len As Integer
Dimensions As Integer
DimTB(1) As FBArrayDim
End Type
So you see, what gets passed when you pass an array is not byref or byval. It's by-descriptor. In recent versions of FB you can pass strings byval, but I think FB just copies the string and passes the new one byref.