Mega Code Archive

 
Categories / Delphi / Examples
 

Understanding pointers for the amateurs

Title: Understanding pointers for the amateurs Question: This article is just for them who dont have any idea of what a pointer is, and does only describe it in the simple way. Answer: Understanding pointers for the amateurs Written for all the beginners out there This article is just for them who dont have any idea of what a pointer is, and does only describe it in the simple way. A pointer is like a web-address, it points to something: a record, an integer, a string, (...). Many API functions have pointers as arguments, like the ShellExecuteEx function. It's delclared like this in the MAPI help file: ShellExecuteEx( LPSHELLEXECUTEINFO lpExecInfo // pointer to SHELLEXECUTEINFO structure ); To manage this, you find out what this structure if built up of: typedef struct _SHELLEXECUTEINFO { // sei DWORD cbSize; ULONG fMask; HWND hwnd; LPCSTR lpVerb; LPCSTR lpFile; LPCSTR lpParameters; LPCSTR lpDirectory; int nShow; HINSTANCE hInstApp; // Optional members LPVOID lpIDList; LPCSTR lpClass; HKEY hkeyClass; DWORD dwHotKey; HANDLE hIcon; HANDLE hProcess; } SHELLEXECUTEINFO, FAR *LPSHELLEXECUTEINFO; Then, you assign values to the fields in a variable of this structure, and simly writes this: ShellExecuteEx(@MyShellExecuteInfo); An example from the internet: Instead of sending the great Delphi-page you found yesterday to a friend, you just send the web-address, so he can find it at the same place. Thats the principle of pointers. Not much, is it? When you use pointers, you use two operators to tell what you mean: @ and ^. See the example under to see how. If you who read this now CAN this, please write a comment, and explain more. I dont know much more than Ive just written! ------------------------------------------------------- A short pointer example: (To test this, choose File|New|Other|Console Application) program UsePointer; {$APPTYPE CONSOLE} type MyPInteger = ^Integer; // This means: MyPInteger is a pointer type to a Integer var X: Integer; procedure PrintOutInt(P: MyPInteger); begin WriteLn(P^); // Find the information at the address of P ReadLn; //Just to hang the program end; begin X := 17; PrintOutInt(@X); // Send the address of X as a parameter. end. Write P1 := P2 if you want to assign the memory address to another pointer. ------------------------------------------------------- About this article and me: Im still a beginner with Delphi and I want to teach those who are at the same level as me what I know. All the articles Ive written till now is at the same level, and (I really hope!) easy to understand. Feel free to take a look at them too, and of course: I also want contact with Delphi programmers at my level (and age: 15). Hint, hint Martin Strand marstr2@online.no (.no means Norway. I understand Norwegian, Swedish, Danish, English and some German.)