News:

Let's find out together what makes a PIC Tick!

Main Menu

structures in basic

Started by bryan, Apr 03, 2021, 09:57 PM

Previous topic - Next topic

bryan

hello, I'd like to know if there's any way to define a structure in basic or some way to emulate it. thank you in advance for the help

top204

A structure type of mechanism can be created using an array, and aliasing the variables within the strucure to the elements of the array. The compiler allows each element of an array to be used as an individual variable, so they can be joined or seperated as Byte, Word, Long, Dword or Float variables using casting, and other arrays using the Dim xxx As XXX.VariableType and Dim xxx As XXX At directive.

keytapper

Unfortunately it's possible to have only one structure. Structure arrays would be a kind of complication.
Ignorance comes with a cost

bryan

thanks for the answers, my query is given because I'm writing a task manager and I thought I'd use a structure to store the pointers of the task functions.

Gabi

Not sure if it helps, you could try something like Les said above:


    ' Union
    ' the size of a union variable will always be the size of its largest member
    ' all members share the same memory space
    '---------------------------------------------------------------------------
    Dim union_A[4] As Byte                                              ' the largest_member_in_union_A
    Dim some_byte_member_in_union_A        As Byte     At  union_A#0
    Dim some_word_member_in_union_A        As Word     At  union_A#0
    Dim some_float_member_in_union_A       As Float    At  union_A#0
   
    ' Structure
    '----------------------------------------------------------------------------
    ' say structure length = 10 bytes ( 2 words + 1 dword + 2 bytes)
    Dim structure_A[10] As Byte
    Dim structure_A_member_0_word   As Word     At  structure_A#0       ' offset + 0
    Dim structure_A_member_1_word   As Word     At  structure_A#2       ' offset + 2
    Dim structure_A_member_2_dword  As Dword    At  structure_A#4       ' offset + 4
    Dim structure_A_member_3_byte   As Byte     At  structure_A#8       ' offset + 8
    Dim structure_A_member_4_byte   As Byte     At  structure_A#9       ' offset + 9
   
   
    ' Array of 3 structures (A/B/C)
    '---------------------------------------------------------------------------
    Dim array_of_struct[12] As Byte
    ' structure A
    Dim struct_A[4]         As Byte At  array_of_struct#0
    Dim struct_A_member_0  As Byte At  struct_A#0
    Dim struct_A_member_1  As Byte At  struct_A#1
    Dim struct_A_member_2  As Byte At  struct_A#2
    Dim struct_A_member_3  As Byte At  struct_A#3
    ' structure B
    Dim struct_B[4]         As Byte At  array_of_struct#4
    Dim struct_B_member_0  As Byte At  struct_B#0
    Dim struct_B_member_1  As Byte At  struct_B#1
    Dim struct_B_member_2  As Byte At  struct_B#2
    Dim struct_B_member_3  As Byte At  struct_B#3
    ' structure C
    Dim struct_C[4]         As Byte At  array_of_struct#8
    Dim struct_C_member_0  As Byte At  struct_C#0
    Dim struct_C_member_1  As Byte At  struct_C#1
    Dim struct_C_member_2  As Byte At  struct_C#2
    Dim struct_C_member_3  As Byte At  struct_C#3
    ' how you fill the members:
    ' probably collection of data in a for-next loop, using the "Array" referencing the respective structure(s),
    ' or direct to member, etc.
    ' and access the members by their individual variable name

Cheers
GL & 73
YO4WM