Here's the code step-by-step that hopefully will help anyone with similar woes:
'udt' is a user defined type, to describe some the contents of a memory location with some particular structure:
Code: Select all
Type udt
As Ulong index = 5
As Ulong counter = 8
End Type
'test' is a variable (stored somewhere in memory) having the structure of 'udt' and can be accessed through the variable and UDT member.
Code: Select all
Dim As udt test
print test.counter
'testudtptr' is a pointer (a memory address) that tells us where test is located in memory, and that the data at that memory location has the structure of 'udt'. The compiler knows what structure to use when 'testudtptr' is accessed because the type of pointer is known by the variable's type:
Code: Select all
Dim as udt ptr testudtptr = @test
print testudtptr->counter
print (*testudtptr).counter
'testptr' is a memory address but by typing it as 'any ptr' there is no expectation of what kind of structure is located at that memory address. The compiler knows that it is a memory location but has been told to forget what ever kind of structure might be there.
To access the members of a structure (UDT) through a pointer, the compiler needs to know what that pointer is pointing to. By casting the any pointer back to the original 'udt' type we can tell the compiler how the pointer (memory location) should be accessed and what structure is located there:
Code: Select all
Type udt
As Ulong index = 5
As Ulong counter = 8
End Type
Dim As udt test
Dim testptr As Any Ptr = @test
print cast( udt ptr, testptr )->counter
print cptr( udt ptr, testptr )->counter
print (*cast( udt ptr, testptr )).counter
print (*cptr( udt ptr, testptr )).counter