Calling Procedures With Structure Arguments

Share your advanced PureBasic knowledge/code with the community.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Calling Procedures With Structure Arguments

Post by BackupUser »

Code updated for 5.20+

Restored from previous forum. Originally posted by Inner.

Code: Select all

#MAX_VALUE=3

Structure TESTSTRUCT
  name.s
  number.l
  array_string.s[#MAX_VALUE]
  array_long.l[#MAX_VALUE]
  array_word.w[#MAX_VALUE]
  array_byte.b[#MAX_VALUE]
EndStructure

mystruct.TESTSTRUCT

mystruct\name="Test String"
mystruct\number=3665412
For i=0 To #MAX_VALUE-1 : mystruct\array_string[i]="String Array "+Str(i) : Next
For i=0 To #MAX_VALUE-1 : mystruct\array_long[i]=Random(100) : Next
For i=0 To #MAX_VALUE-1 : mystruct\array_word[i]=Random(100) : Next
For i=0 To #MAX_VALUE-1 : mystruct\array_byte[i]=Random(100) : Next

Procedure Display_TESTSTRUCT(*outstruct.TESTSTRUCT)
  Debug(*outstruct\name)
  Debug(*outstruct\number)
  For i=0 To #MAX_VALUE-1 : Debug(*outstruct\array_string[i]) : Next
  For i=0 To #MAX_VALUE-1 : Debug(".L Array "+Str(*outstruct\array_long[i])) : Next
  For i=0 To #MAX_VALUE-1 : Debug(".W Array "+Str(*outstruct\array_word[i])) : Next
  For i=0 To #MAX_VALUE-1 : Debug(".B Array "+Str(*outstruct\array_byte[i])) : Next
EndProcedure

Display_TESTSTRUCT(@mystruct)
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Franco.

Keep in mind, you don't copy the values from mystruct to outstruct.
You only pass the pointer to the procedure, mystruct and outstruct share the same pointer.
If you change the values in the mystruct structure also the values getting from outstruct are changed!

If you need access to a structure inside a procedure you have to use the shared command:

Code: Select all

Structure TESTSTRUCT
    name.s
    number.l
EndStructure

mystruct.TESTSTRUCT

mystruct\name="Test String"
mystruct\number=3665412

Procedure WhatEver()
  Shared mystruct
  Debug(mystruct\name)
  Debug(mystruct\number)
EndProcedure

Have a nice day...

Franco
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by freak.

@Franco,
The shared Command actually does exactly the same, if you now change some Value in the Procedure, it is also changed in the main Prog, because now it is not only the same pointer, but also the same variable :)

What you said was right though...

Timo
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by tinman.

Does this qualify as a tip or a trick? Haven't we all been doing this for a long time?

No offence meant to Inner.


--
I used to be a nihilist but I don't believe in that any more.
(Win98first ed. + all updates, PB3.51, Ed3.53)
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by blueb.

Question:

How about:

Code: Select all

 Global mystruct.TESTSTRUCT
Would that work as well, that way you wouldn't worry about sharing variables in procedures?

--blueb
Post Reply