passing structure in structure to procedure

Just starting out? Need help? Post your questions and find answers here.
tryforsure
New User
New User
Posts: 6
Joined: Tue Nov 27, 2007 12:19 am
Location: Scotland

passing structure in structure to procedure

Post by tryforsure »

Hi, I've been checking out purebasic for a while now and really like it for prototyping.

I've run into to a problem, which i'm sure there is an easy answer too, but, because of my lack of knowledge in purebasic, I'm having difficulty!
so if anyone could offer some enlightenment it would be much appreciated

I have three structs

Code: Select all

Structure dataStats
        max,i
        min.i
        longest.d
        shortest.d
endstructure

Structure testStats
         stat1.dataStats ;<- variable of type struct 
         Array ghouls.w (6)
Endstructure

Structure boreStats
        stat2.dataStats ;<- variable of type struct 
        wami.i
        freq.i
        Array fiends.w(10)
EndStructure

Global Dim idx.testStats (4) ; i creat globals to ease any hassle
Global Dim odx.boreStats (7)
as you can see above both structures contain a structure of type dataStats.
and both contain arrays

can anyone say how i can just pass the variable of type struct contained in the other structures
to a function, and also, how i can pass only the array to a procedure
i.e.

Code: Select all

Procedure testfiends( *pointer_to_structure.dataStats)
;update le stats
*pinter_to_structure\longest= 2
endprocedure

Procedure checkarray( *pointer_to_array )
;update le array
test = ArraySize( *pointer_to_array )
*pointer_to_array(2) = 4
endprocedure

testfiends ( @odx\stat2 ) ; ? like this does not work for me or @(odx\stat2)
testfiends ( @idx\stat1 )
checkarray ( @odx.fiends() ) ; pass arrays?
I'm sorry its very long winded but its been keeping me up at night, lol.

any insight is appreciated

best regards
Dazbo
User avatar
skywalk
Addict
Addict
Posts: 4242
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: passing structure in structure to procedure

Post by skywalk »

small snippet of your code... :wink:

Code: Select all

Procedure checkarray(Array artest.testStats(1), Array arbore.boreStats(1))
  ;update le array
  test = ArraySize(artest())
  artest(1)\stat1\max = 4
EndProcedure

checkarray(idx(), odx()) ;<-- pass arrays to procedure
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
citystate
Enthusiast
Enthusiast
Posts: 638
Joined: Sun Feb 12, 2006 10:06 pm

Re: passing structure in structure to procedure

Post by citystate »

perhaps something like this:

Code: Select all

Procedure testfiends( *pointer_to_structure.dataStats)
;update le stats
*pointer_to_structure\longest= 2
EndProcedure

Procedure checkarray( Array my_array.w(1) ); you need to include at least one dimension in passed arrays
  ;update le array
  test = ArraySize( my_array() )
  my_array(2)=4
EndProcedure

x=2

testfiends ( odx(x)\stat2 ) ; you've created two arrays with your Dim statements, you'll need to include
testfiends ( idx(x)\stat1 ) ; an index (in this case, I've used a variable 'x' )

checkarray ( odx(x)\fiends() ) ; pass arrays like this
there is no sig, only zuul (and the following disclaimer)

WARNING: may be talking out of his hat
tryforsure
New User
New User
Posts: 6
Joined: Tue Nov 27, 2007 12:19 am
Location: Scotland

Re: passing structure in structure to procedure

Post by tryforsure »

Thanks a lot citystate that worked, first time, with out a problem, I'm sure I had tried variations on what you put but I must have been too tired.

BTW
citystate wrote:; you've created two arrays with your Dim statements, you'll need to include
; an index (in this case, I've used a variable 'x' )
I had just forgot to add these when typing the example.


Just one other question can I have a pointer to an array within a struct which does not have an array keyword?
as in

Code: Select all

Array happy.d(10)

Structure be
 *pointer
 test.i
Endstructure
lets.be

lets\pointer = @happy()
User avatar
luis
Addict
Addict
Posts: 3895
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: passing structure in structure to procedure

Post by luis »

A pointer contains a number, so obviously you can have a pointer in a structure to anything.
If the number is the base address of an array or the phone number of your girlfriend PB doesn't care. :wink:

Code: Select all

Dim happy.d(10) ; DIM not ARRAY 

happy(0) = 123.25
happy(9) = 256.25

Structure be
 *pointer
 test.i
EndStructure

lets.be

lets\pointer = @happy()

Debug PeekD(lets\pointer)
Debug PeekD(lets\pointer + SizeOf(double) * 9)
"Have you tried turning it off and on again ?"
tryforsure
New User
New User
Posts: 6
Joined: Tue Nov 27, 2007 12:19 am
Location: Scotland

Re: passing structure in structure to procedure

Post by tryforsure »

luis wrote: you can have a pointer in a structure to anything
yes I see, this is how I would expect it to work
luis wrote:

Code: Select all

Debug PeekD(lets\pointer + SizeOf(double) * 9)
and I notice that you have included some pointer arithmetic, which is very interesting.

thanks, very much, for your help.

ps.
luis wrote: DIM not ARRAY
I think "dim not awake" is more apt at times, lol.
User avatar
luis
Addict
Addict
Posts: 3895
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: passing structure in structure to procedure

Post by luis »

If you want to get an headache you can read this thread where we were discussing pointers arithmetic in PB (and its limitations)

http://www.purebasic.fr/english/viewtop ... =3&t=48234

It's informative even if a little...disturbing... :lol:

And the help page on pointers here (also in the help file you got with PB) -> http://www.purebasic.com/documentation/ ... emory.html
"Have you tried turning it off and on again ?"
tryforsure
New User
New User
Posts: 6
Joined: Tue Nov 27, 2007 12:19 am
Location: Scotland

Re: passing structure in structure to procedure

Post by tryforsure »

luis wrote:If you want to get an headache you can read this thread where we were discussing pointers arithmetic in PB (and its limitations)
http://www.purebasic.fr/english/viewtop ... =3&t=48234
Excellent this was the type of info I was looking for, the fifth post by skywalk showed me some great info with regards to pointers specifically; being able to index a pointer using the square brackets.

thanx again!
Post Reply