This isn't something you might have come across unless working with C structs & max types, but it caused me a few hours of head scratching.
When working in C, the field data of an array of structs is sequential in memory. In Blitzmax this is not the case. Here's how you can get a 'C friendly' array of your Blitzmax type array data:
When working in C, the field data of an array of structs is sequential in memory. In Blitzmax this is not the case. Here's how you can get a 'C friendly' array of your Blitzmax type array data:
Type test Field a:Byte,b:Byte Function GetArray:Byte[](srcArray:test[] Var) Local sz:Int = SizeOf(test) Local dataArray:Byte[srcArray.length * sz] Local i:Int Local bPtr:Byte Ptr = Varptr(dataArray[0]) For i = 0 Until srcArray.length MemCopy bPtr+i*sz, Varptr(SrcArray[i].a),sz Next Return dataArray End Function End Type 'Create a 'test' array Local t:test[3] t[0] = New test t[1] = New test t[2] = New test t[0].a=1 t[0].b=2 t[1].a=3 t[1].b=4 t[2].a=5 t[2].b=6 Local tPtr:Byte Ptr = Varptr(t[0].a) 'Pointer to first byte in the array Local i Print "In Blitzmax type arrays, the fields are not sequential in memory as you might think" For i = 0 Until 6 Print tPtr[i] Next Print 'A 'C' struct friendly array Local fieldData:Byte[] = test.GetArray(t) Print "Using myType.GetArray() you can get a byte[] of" Print "your type array data fields that will be 'C friendly" For i = 0 Until fieldData.length Print fieldData[i] Next 'Typically you would pass varptr(fieldData) to your C functions