Just starting out? Need help? Post your questions and find answers here.
srod
PureBasic Expert
Posts: 10589 Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...
Post
by srod » Tue May 16, 2006 9:11 pm
The following runs fine:
Code: Select all
Dim a(10)
Dim b(10)
a(0) = 1
a(1) = 2
b()=a() ;b should now point to a.
Debug b(1) ;Displays 2 as expected.
However, the following does not display the correct result. It should be functionally identical to the above.
Code: Select all
Dim a(10)
a(0) = 1
a(1) = 2
Procedure Test(addy)
Dim b(10)
b()=addy
Debug b(1) ;Should display 2 !
EndProcedure
Test(a())
??? I'm probably doing something daft!
Last edited by
srod on Tue May 16, 2006 10:49 pm, edited 1 time in total.
I may look like a mule, but I'm not a complete ass.
Tranquil
Addict
Posts: 950 Joined: Mon Apr 28, 2003 2:22 pm
Location: Europe
Post
by Tranquil » Tue May 16, 2006 9:18 pm
Are you sure that A() = B() is a valid operation!? Never thought about this. Sure, A() returns something like a pointer. Is this new on PB4?
Tranquil
Paul
PureBasic Expert
Posts: 1252 Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:
Post
by Paul » Tue May 16, 2006 9:29 pm
Remember that Dim is no longer Global by default in PureBasic 4.
Did you try adding Global in front of both your Dim commands so the data can be shared?
srod
PureBasic Expert
Posts: 10589 Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...
Post
by srod » Tue May 16, 2006 9:38 pm
Thanks for the replies.
Paul: I wish to avoid using global arrays at the moment, but that would of course solve this problem - although it doesn't explain why the code doesn't run as is!
Tranquil: no, it's not new on PB4, I used to use this all the time with PB3.94 etc. Although this was of course where all arrays were global etc.
Strange, the statement
has no affect when used in a procedure, even if both arrays are DIMensioned within the procedure itself. This would appear to be the root of the problem.
I may look like a mule, but I'm not a complete ass.
Tranquil
Addict
Posts: 950 Joined: Mon Apr 28, 2003 2:22 pm
Location: Europe
Post
by Tranquil » Tue May 16, 2006 9:45 pm
I searched through the whole PB referenze and I can not find any hint that your expression is valid.
Maybe its a bug that it even works. Normaly the () indicates a linked list.
Tranquil
josku_x
Addict
Posts: 997 Joined: Sat Sep 24, 2005 2:08 pm
Post
by josku_x » Tue May 16, 2006 9:47 pm
Well, be quiet before Fred fixes that bug
srod
PureBasic Expert
Posts: 10589 Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...
Post
by srod » Tue May 16, 2006 9:56 pm
I think it's well known that a() gives the base address of the array. Indeed, in PB4, you use this to pass an array to a procedure. The expression b()=a() is probably not documented (or encouraged
), however I've seen it used quite a lot.
Oh well, I'll need to take another tact all together.
I may look like a mule, but I'm not a complete ass.
Kale
PureBasic Expert
Posts: 3000 Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:
Post
by Kale » Tue May 16, 2006 10:03 pm
--Kale
netmaestro
PureBasic Bullfrog
Posts: 8433 Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada
Post
by netmaestro » Tue May 16, 2006 10:07 pm
If you were going to code that, I believe this would be right:
Code: Select all
Dim a(10)
a(0) = 1
a(1) = 2
Procedure Test(addy(1))
Dim b(10)
b()=addy()
Debug b(1) ;Should display 2 !
EndProcedure
Test(a())
However, it fails because even though the procedure knows the correct address of the a() array, it refuses to assign it to b() because they do not share the same scope. So I don't believe it is expected to work.
BERESHEIT
srod
PureBasic Expert
Posts: 10589 Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...
Post
by srod » Tue May 16, 2006 10:08 pm
Yea, I looked at that. It could be (at least in that I guess the code I'm using is probably to be discouraged!) Still, it's strange that the 'trick' works outside of procedures, but not within.
I may look like a mule, but I'm not a complete ass.
srod
PureBasic Expert
Posts: 10589 Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...
Post
by srod » Tue May 16, 2006 10:11 pm
netmaestro, the following doesn't work either although both arrays share the same scope:
Code: Select all
Procedure Test()
Dim a(10)
Dim b(10)
a(0) = 1
a(1) = 2
b()=a()
Debug b(1) ;Should display 2 !
EndProcedure
test()
I may look like a mule, but I'm not a complete ass.
josku_x
Addict
Posts: 997 Joined: Sat Sep 24, 2005 2:08 pm
Post
by josku_x » Tue May 16, 2006 10:11 pm
Try with Macro? maybe it works
netmaestro
PureBasic Bullfrog
Posts: 8433 Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada
Post
by netmaestro » Tue May 16, 2006 10:12 pm
You probably won't like this because it copies the whole array, but I'll offer it anyway just in case:
Code: Select all
Dim a(10)
a(0) = 1
a(1) = 2
size = PeekL(@a()-8) * SizeOf(long)
Procedure Test(loc,size)
Dim b(10)
CopyMemory(loc,b(),size)
Debug b(1) ;Should display 2 !
EndProcedure
Test(a(),size)
BERESHEIT
netmaestro
PureBasic Bullfrog
Posts: 8433 Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada
Post
by netmaestro » Tue May 16, 2006 10:25 pm
This works:
Code: Select all
Dim a(10)
a(0) = 1
a(1) = 2
Procedure Test(addy)
Static Dim b(10)
b()=addy
Debug b(1) ;Should display 2 !
EndProcedure
Test(a())
BERESHEIT
josku_x
Addict
Posts: 997 Joined: Sat Sep 24, 2005 2:08 pm
Post
by josku_x » Tue May 16, 2006 10:26 pm
so it's all about using static, right?
these *undocumented* things drive me crazy