Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Array of Pointers? Or is it a Pointer to an array?
#1
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]
quote="Deleter"]judging gameplay, you can adaquately compare quake 4 with pong[/quote]
Reply
#2
it's asking for an array of pointers, try byref if you just want the array by reference Tongue
ttp://m0n573r.afraid.org/
Quote:quote: "<+whtiger> you... you don't know which way the earth spins?" ... see... stupidity leads to reverence, reverence to shakiness, shakiness to... the dark side
...phear
Reply
#3
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.
Reply
#4
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?
quote="Deleter"]judging gameplay, you can adaquately compare quake 4 with pong[/quote]
Reply
#5
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.
hen they say it can't be done, THAT's when they call me ;-).

[Image: kaffee.gif]
[Image: mystikshadows.png]

need hosting: http://www.jc-hosting.net
All about ASCII: http://www.ascii-world.com
Reply
#6
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]
quote="Deleter"]judging gameplay, you can adaquately compare quake 4 with pong[/quote]
Reply
#7
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.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)