how do i pass an array as a function argument in purebasic?

Just starting out? Need help? Post your questions and find answers here.
pieter
New User
New User
Posts: 6
Joined: Sat Jun 14, 2003 9:16 pm

how do i pass an array as a function argument in purebasic?

Post by pieter »

hello,

how do i pass an array as a function argument in purebasic?

seems a dumb question, i know (:-)

pieter
[-pg-]
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

One way is to pass the address of the array.

Code: Select all

#NoElements = 10

Procedure testarray(AddressOfArray.l)
  Dim TempArray.s(0)
  TempArray() = AddressOfArray

  Debug TempArray(1)
  TempArray(1) = "Goodbye!"

EndProcedure



Dim a.s(#NoElements)
a(1) = "Hello!"
testarray(a());  NOTE that a() gives the address of the array a.
Debug a(1)
End
Note that we are effectively passing by reference here.
I may look like a mule, but I'm not a complete ass.
pieter
New User
New User
Posts: 6
Joined: Sat Jun 14, 2003 9:16 pm

how do i pass an array as a function argument in purebasic?

Post by pieter »

Thanks srod,

I will give it a try, even though it still seems a little bit odd to me. Probably because I excpect behaviour simillar to other programming languages.

Cheers,
Pieter Greyling
[-pg-]
Shannara
Addict
Addict
Posts: 1808
Joined: Thu Oct 30, 2003 11:19 pm
Location: Emerald Cove, Unformed

Post by Shannara »

Ok, how would I be able to redim the TempArray with a new size after it is mapped to the AddressofArray? Especially if I do the following:

Code: Select all

Dim TempArray.s(25)
It will erase it's map to the AddressofArray..

I need to be able to resize the array the pointer is pointing to whenever I need to, within this routine, via pointer.
Post Reply