Pointer types in DelphiA variable - stores data at a particular memory address. Normally you don't know or care what that memory address is.
However if you want to find out, you can use the reference operator @. This is a mini-function that will return the actual address of any variable you provide to it. You can store memory addresses in a special type of variable called a pointer. A pointer is simply a variable that contains an address. Create a standard vanilla pointer variable as you would create any other type of variable var However there's a special, extended way to create pointers which also stores information about the type of variable that they are going to point to. To create these, use this method:
var These extended pointers are allowed to do things which plain vanilla pointers cannot. This is because Delphi is protective and won't allow pointers in procedures such as writeln() unless it knows what kind of thing it is.
Here's an example. We create a string and a pointer to a string, set up the pointer to ... well, point at the string by finding the string's address using @, the reference operator. Once we have the address we assign it to the pointer. The pointer now points at the string. We can reverse the process, using the dereference operator (a ^ at the end of the pointer name) to get the contents of the variable that the pointer is pointing to, and print it out. Of course we could just print the string, but that would be too easy :) uses Make your own version of this program and try two things:
Notice that there's a potential for confusion here (understatement). The symbol ^ is doing two quite different jobs depending on where you find it. In it's first incarnation it appears in front of a type, to indicate that the requested variable is a pointer to the type rather than the actual type itself. It's short for 'is a pointer to ...' In the second incarnation it appears at the end of a pointer variable name to indicate that what's needed is the value stored in the variable that the pointer is pointing at. Is that clear? Didn't think so. It takes some getting used to.
More information |