FreeBasic Bare Bones: Difference between revisions

Jump to navigation Jump to search
[unchecked revision][unchecked revision]
Content deleted Content added
No edit summary
m Bot: Replace deprecated source tag with syntaxhighlight
Line 20: Line 20:


In FreeBasic, a pointer is defined by adding the Ptr keyword.
In FreeBasic, a pointer is defined by adding the Ptr keyword.
<source lang="freebasic">
<syntaxhighlight lang="freebasic">
Dim mypointer As Byte Ptr
Dim mypointer As Byte Ptr
</syntaxhighlight>
</source>
This one holds the location of a byte of memory.
This one holds the location of a byte of memory.
<source lang="freebasic">
<syntaxhighlight lang="freebasic">
Dim mypointer As Long Ptr
Dim mypointer As Long Ptr
</syntaxhighlight>
</source>
This one points to the first of 4 bytes of memory, which together form the number.
This one points to the first of 4 bytes of memory, which together form the number.


You can not use pointers straight away. Consider sending a letter with no address on it will not do any good. So we must first put an address in there. That leads to the question: how do we get an address. Some things have fixed addresses, like the video card.
You can not use pointers straight away. Consider sending a letter with no address on it will not do any good. So we must first put an address in there. That leads to the question: how do we get an address. Some things have fixed addresses, like the video card.
You can also ask variables for their addresses.
You can also ask variables for their addresses.
<source lang="freebasic">
<syntaxhighlight lang="freebasic">
Dim variable as Byte
Dim variable as Byte
Dim pointer as Byte Ptr
Dim pointer as Byte Ptr
pointer = @variable
pointer = @variable
</syntaxhighlight>
</source>
The @ returns the address of the variable that follows it.
The @ returns the address of the variable that follows it.
<source lang="freebasic">
<syntaxhighlight lang="freebasic">
Dim pointer as Byte Ptr
Dim pointer as Byte Ptr
Dim address as Long
Dim address as Long
address = 12345
address = 12345
pointer = CPtr(Byte Ptr, address)
pointer = CPtr(Byte Ptr, address)
</syntaxhighlight>
</source>
Here CPtr (Convert to Pointer) is used to create pointers. You give it a type (Byte Ptr) and the address. You can also change pointers:
Here CPtr (Convert to Pointer) is used to create pointers. You give it a type (Byte Ptr) and the address. You can also change pointers:
<source lang="freebasic">
<syntaxhighlight lang="freebasic">
Dim pointer1 as Byte Ptr
Dim pointer1 as Byte Ptr
Dim pointer2 as Long Ptr
Dim pointer2 as Long Ptr
pointer2 = CPtr(Long Ptr, pointer1)
pointer2 = CPtr(Long Ptr, pointer1)
</syntaxhighlight>
</source>
Be careful when you do so: a byte occupies one location in memory, a long occupies four. if we would use this pointer, we would use three locations in memory of which we know nothing about. Sometimes, they are necessary, like when we want to work with strings without having the Runtime.
Be careful when you do so: a byte occupies one location in memory, a long occupies four. if we would use this pointer, we would use three locations in memory of which we know nothing about. Sometimes, they are necessary, like when we want to work with strings without having the Runtime.


You can access pointers with an index:
You can access pointers with an index:
<source lang="freebasic">
<syntaxhighlight lang="freebasic">
Dim value as Long
Dim value as Long
Dim pointer as Long Pointer
Dim pointer as Long Pointer
pointer = @value
pointer = @value
' value = pointer[0]
' value = pointer[0]
</syntaxhighlight>
</source>
when we want to know what is behind the pointer, we ask for the memory at its location with the [ ], and then we add a number which tells us how many locations further to look. When we use [0] it simply means that we do not want to do anything with the address stored in the pointer. The result is the value stored in the memory locations pointed to by the pointer. For byte pointers, this will look at the address stored and return the result. For other pointers, it will look at a series of locations and return what those values represent.
when we want to know what is behind the pointer, we ask for the memory at its location with the [ ], and then we add a number which tells us how many locations further to look. When we use [0] it simply means that we do not want to do anything with the address stored in the pointer. The result is the value stored in the memory locations pointed to by the pointer. For byte pointers, this will look at the address stored and return the result. For other pointers, it will look at a series of locations and return what those values represent.


Strings are useful, but tedious. A string is stored as a series of bytes in consecutive addresses. You can build a pointer to a string as well:
Strings are useful, but tedious. A string is stored as a series of bytes in consecutive addresses. You can build a pointer to a string as well:
<source lang="freebasic">
<syntaxhighlight lang="freebasic">
Dim s as String
Dim s as String
Dim pointer as String Ptr
Dim pointer as String Ptr
pointer = @s
pointer = @s
</syntaxhighlight>
</source>
But since we do not have the Runtime, we have to use something that does not use it.
But since we do not have the Runtime, we have to use something that does not use it.
<source lang="freebasic">
<syntaxhighlight lang="freebasic">
Const s = "Text"
Const s = "Text"
Dim pointer as Byte Ptr
Dim pointer as Byte Ptr
Line 76: Line 76:
' pointer[3] = Asc("t")
' pointer[3] = Asc("t")
' pointer[4] = 0
' pointer[4] = 0
</syntaxhighlight>
</source>
The first example uses the Runtime which will not compile. The second example shows the only working method of using strings: Define one using Const, then ask for a pointer to that. Since a string is a sequence of characters (bytes), we change the type of the pointer in the process. Next we can ask for individual characters as if it were an array. However, they now appear as numbers. The ASCII codes to be precise, and the same as when you use Chr$() and Asc().
The first example uses the Runtime which will not compile. The second example shows the only working method of using strings: Define one using Const, then ask for a pointer to that. Since a string is a sequence of characters (bytes), we change the type of the pointer in the process. Next we can ask for individual characters as if it were an array. However, they now appear as numbers. The ASCII codes to be precise, and the same as when you use Chr$() and Asc().