47
calling procedures, so pointers are used instead. There are two ways to pass pointer data
to a function; by explicitly declaring a pointer data type which you will see in the chapter
on pointers, or by using the AddressOf operator which is explained here.
The AddressOf Operator @
When the compiler creates a variable, it allocates a portion of memory equal to the
size of the data type. For an integer it would allocate 4 bytes and for a double it would
allocate 8 bytes. Each memory location has an address associated with it so that the CPU
can move data into and out of the memory location. When the compiler allocates the
memory, the operating system returns an address for the memory location so that the
compiler can access that location. When you are working with variables you are actually
working with an addressable memory location in the computer. You don't have to worry
about the address since the compiler handles all those details for you; you can just use
the variable in your program.
However, when you need to call a function like Modf that requires a pointer to a
variable, then you need to know the address of the variable so you can pass it to the
function. Why can't you just use the variable name, since it is an alias for the memory
location? The answer is in the declaration of Modf shown in listing 5.3.
1
declarefunctionmodfcdeclalias"modf"(byvalasdouble,byvalasdoubleptr)asdouble
Listing 4.2: Modf Declaration
Notice that the second parameter is defined as byval and the data type is a pointer
(ptr) to a double-type variable. Remember that byval means “make a copy of the
contents of the parameter and use it in the function.” If you were to simply use the
variable name, you would be passing the contents of the memory location, whatever is in
the variable,and not its address.
When you use a regular variable in your program, you are working with the actual
data in the memory location associated with the variable. That is, a variable = data. If
you define myInt as an integer and set it equal to 5, when you print myInt to the screen
you will see 5. The pointer data type is unique in that what it contains isn't data, that is a
value like 134, rather it contains the address of a memory location. A pointer = memory
address. If you were to print a pointer to the screen you would see a number that doesn't
make much sense, since what you are looking at isn't the data in the memory location,
but the address of the data in memory.
When you first create a pointer, it doesn't point to anything. This is called a NULL
pointer. There are a number of ways to initialize the pointer, but one method is to use the
AddressOf operator. If you create a pointer variable such as myIntPtr you could initialize
that pointer with code like myIntPtr=@myInt. The @ is the AddressOf operator, and
this code snippet sets myIntPtr to the address of myInt. In other words, an initialized
pointer variable and the AddressOf operator both return the same value, a memory
address.
We can use this concept in the Modf function. Rather than creating a pointer
variable, we can create a double-type variable and then use this variable with the
AddressOf operator for the return parameter. Remember that you were able to use
expressions with the built-in numeric conversion functions. Using the AddressOf operator
with a variable is an expression which will be evaluated and the result, the variable
address, will be passed along to the function.
43