Acces to an array of a structure via pointer

Just starting out? Need help? Post your questions and find answers here.
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 sawa.

Hi everybody,

I saw below the topic "Procedure arguments" and I askeds myself how can I get access to an array of a structure via pointer.

i.e.

Structure Stru
st.s
lo.l
EndStructure

Dim Array1.Stru(10)

FillArray(Array1, 10) ; How can I get access to the single
;elements of the structure within this function


Or the next step: An array of a structure which includes an array
i.e.
Structure Stru
st.s
lo.l[10]
EndStructure

Dim Array1.Stru(10)


I tried several ways, but no one worked.

Sawa
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 Pupil.

If you want to access the array via pointers inside a procedure you probably could do something like this:

Code: Select all

Structure Stru
  st.s
  lo.l
EndStructure

Dim Array1.Stru(10)

Procedure FillArray(*ptr.Stru, elements.l)
  For i = 0 to elements
    *ptr\st = Str(i)
    *ptr\lo = i
    *ptr+SizeOf(Stru)
  Next
EndProcedure

FillArray(Array1(), 10)
If your structure has an array you just access like this(using pointer) *ptr\lo[1] = 12
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 sawa.

Thanks pupil,

It works inside PureBasic. I made a lot of tries with MS-Basic, but I didn't find a solution. I'm not sure if the problem is PureBasic or MS-Basic. I assume it is MS-Basic.

Does anybody has experience in calling dll's and passing arrays as parameter?

Here is my example:

;-------------------
;PureBasic
;-------------------

Structure Stru
st.s
ia.w
EndStructure


ProcedureDLL FillArray(*ptr.Stru)

MessageRequester("Int",Str(*ptr\ia),0)
MessageRequester("String",*ptr\st,0)
EndProcedure



'-------------------
'MS-Basic call
'-------------------

Declare Sub FillArray lib "D:\Entwicklung\Mlle\RulesDLL\StruTest\PureBasic.dll" Alias "_FillArray" (a() As Stru)

Type Stru
st As Integer
ia As Integer
End Type


Public Sub main()

Dim a_ar(10) As Stru

a_ar(0).ia = 122
a_ar(0).st = 0
FillArray a_ar()

End Sub
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 Danilo.

Code: Select all

PureBasic:   Structure Stru
     st.s
     ia.w
   EndStructure

MS-Basic:
   Type Stru
     st As Integer
     ia As Integer
   End Type
Better use the same structures for both BASIC´s.
Your DLL-Call should crash because there is no
String in your MS-BASIC code.

cya,
...Danilo

(registered PureBasic user)
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 sawa.

Yes,

You are right. It happend when I pasted it during my tests,
but the problem stays the same.
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 Pupil.

Is the type 'Integer' in MS Basic a double word (in PB '.l') or a word (in pb .w) because the string pointer in PureBasic is a double word but on the MS Basic side you have declared this as a 'Integer'. But if an 'Integer' indeed is a double word the other structure element in PureBasic is declared wrong.

If Integer is a double type declare structure like this in PureBasic:

Code: Select all

   Structure Stru
     st.s
     ia.l
   EndStructure
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 sawa.

I found out, that I also have problems to change a numeric value in a DLL function. I get the value, but I can't manipulate it.
I will post again when I tested step by step the pointer passing.

Thank you Danilo & Pupil
Post Reply