The Win32 API makes extensive use of pointers. An understanding of pointers is important for successfully using the Win32 API. This tutorial provides a quick introduction to the concept of pointers in the Object Pascal programming language. A sample program is developed to aid in understanding.
At the end of this tutorial you will:
If you are already familiar with pointers, go to the next tutorial: Common Windows API Data Types.
Instead of containing data a pointer contains the address of the data. This can be very confusing, for the new C programmer and for the Object Pascal programmer who seldom has to worry about pointers. Once you understand the concept though, you will find that accessing API functions becomes significantly easier.
All computer data is stored in memory and each variable you declare assigns a memory location to store the variable data. A variable is therefore just a name that your program uses to access a particular region of computer memory. In Object Pascal when you are declaring an integer variable, your program assigns a block of 4 contiguous bytes of memory to store the integer data.
|
I : Integer; |
// declares an integer variable i and allocates 4 bytes of memory. |
You can define a pointer to an integer value using Object Pascal as follows:
|
Pint : ^Integer; |
// declares a variable Pint that will hold the address of an integer value. |
The difference is that the first declaration declared an integer I and allocated memory. The second declaration did not allocate any memory. This is because Pint is not a declaration for an integer, it is a declaration for a variable that will hold the address of an integer. You cannot assign a value directly to Pint until you allocate memory to hold the data and set Pint to point to that address.
Try adding the following code in Delphi and running the program.
|
var Pint : ^Integer; // declare a variable Pint that will hold the address of an integer value. begin Pint := 25; end; |
You probably received a message that says something like [Error] Unit1.pas(29): Incompatible types: 'Integer' and 'Pointer'.
Now try the following:
|
Var I : Integer; // declare an integer variable I Pint : ^Integer; // declare a variable Pint that will hold the address of an integer value. Begin Pint := @I; // assign the address of I to Pint. Pint^ := 25; // place the integer value 25 into the address pointed to by Pint ShowMessage(IntToStr(I)); // display I end; |
And you should get something like this

It worked! The first line (Pint := @I) assigned the address of I to Pint. The second line (Pint^ := 25) placed the value 25 into the 4 byte memory location addressed by Pint. The ^ dereferences Pint – that is it stored the data in the address pointed to by Pint, not into Pint itself. The third line simply displays the contents of the variable I. Notice how the contents of I contained the value 25. This is because Pint contained the address of I.
In Object Pascal we use the symbol ^ to represent a pointer. When it appears before a type identifier as in ^integer, it denotes a pointer to a variable. When we use it after a pointer variable such as Pint^, it returns the value stored at the memory address held by the pointer.
Understanding pointers makes using Windows API functions easier. Object Pascal allows us to use pointers using the symbol ^. When ^ appears before a type identifier as in ^integer, it denotes a pointer to a variable. When we use it after a pointer variable such as Pint^, it returns the value stored at the memory address held by the pointer.
The next tutorial in this series provides A C Primer for Delphi Programmers.
|
Delphi 6 online help |
|
|
WIN32.HLP |
|
|
|
Borland Delphi Run-time Library - Win32 API Interface Unit. |
|
Borland Delphi Run-time Library - Win32 API Shell objects Interface Unit. |
|
|
Win32 common controls interface unit |
|
|
Other tutorials in this series |
|
|
An excellent cooperative effort among Delphi programmers with lots of free source code |
|
The MER System Database Search |
A free newsgroup search engine you can use to find Delphi newsgroup postings |
|
|
The Microsoft support site for developers |
|
|
Borland’s support site. |
|
Newsgroups |
Borland Delphi newsgroups. |
The Tomes of Delphi: Win32 Core API - Windows 2000 Edition by John Ayers, ISBN 1-55622-750-7 Wordware Publishing Inc
The Tomes of Delphi: Win32 Shell API – Windows 2000 Edition by John Ayers, ISBN 1-55622-749-3 Wordware Publishing Inc
Windows 2000 SYSTEMS PROGRAMMING Black Book by Al Williams, 2000, ISBN 1-57610-280-7, The Coriolis Group
The source code contained in this tutorial is provided free of charge.
You may incorporate this source code into your own program but you do so at
your own risk. Please remember to test your program and any source code copied
from this site. Please report errors and improvements to jpgriffiths.com.
Copyright © 2003 Paul Griffiths, www.jpgriffiths.com.
Last Updated: [January 2003]