Visit our Sponsor   Visit our Sponsor
delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi







Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (3)


Understanding pointers – for the amateursFormat this article printer-friendly!Bookmark function is only available for registered users!
Written for all the beginners out there
Product:
Delphi all versions
Category:
Object Pascal
Skill Level:
Scoring:
Last Update:
05/04/2003
Search Keys:
delphi delphi3000 article borland vcl code-snippet Pointer PInteger @ ^ ShellExecuteEx
Times Scored:
3
Visits:
3216
Uploader: Martin Strand
Company:
Reference: Borland Delphi help file
 
Question/Problem/Abstract:
This article is just for them who don’t 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 don’t 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. That’s 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 don’t know much more than I’ve 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: I’m 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 I’ve 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.)





Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment
Rating
    Martin Strand (May 13 2003 8:46PM)

I see you rate this article down. Great! That means that someone knows more than descriped here, so I tell you:
Write comments to the article, so that it can be complete!

This would benefit you (Maybe a free silver membership) and everyone that not know what a pointer is!
Respond

Mora about pointers
    Martin Strand (May 6 2003 8:20PM)

Pointers can be used without static variables. Watch here! The comments explains the code:

program PointerExample;

{$APPTYPE CONSOLE}

uses
  SysUtils;

var
  PMyInt: PInteger;

begin
  New(PMyInt);        // Allocates memory for an Integer
  ReadLn(PMyInt^);    // Sets the value of the dynamic variable to the user's input
  WriteLn(PMyInt^);   // Prints the value of the variable PMyInt points to
  Dispose(PMyInt);    // Frees the memory allocated by the dynamic variable
  ReadLn;
end.

// In this example, the pointer is the only static variable, and by the New procedure, I allocs place for an Integer variable in the memory, and let the PMyInt pointer point to this address. When we not need the Integer anymore, we dispose the pointer, and the momory allocated can be used for other things.
Respond

RE: Mora about pointers
back bacon (Dec 5 2004 11:56PM)

Wow!!  You saved 4 whole bytes of memory and only had to write two more lines of code!  I'm changing my whole application to use pointers right now....

NOT!
Respond














 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
A. B. Talal
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)