delphi中指针操作符^的使用

To see how pointers work, look at the following example.
1    var
2      X, Y: Integer;   // X and Y are Integer variables
3      P: ^Integer;     // P points to an Integer
4    begin
5      X := 17;         // assign a value to X
6      P := @X;         // assign the address of X to P
7      Y := P^;         // dereference P; assign the result to Y
8    end;
  Line 2 declares X and Y as variables of type Integer. Line 3 declares P as a
pointer to an Integer value; this means that P can point to the location of X or
Y. Line 5 assigns a value to X, and line 6 assigns the address of X (denoted by
@X) to P. Finally, line 7 retrieves the value at the location pointed to by P
(denoted by ^P) and assigns it to Y. After this code executes, X and Y have the sa
me value, namely 17.The @ operator, which we have used here to take the address
of a variable, also operates on functions and procedures. For more information,
see The @ operator and Procedural types in statements and expressions.

The symbol ^ has two purposes, both of which are illustrated in our example.
When it appears before a type identifier--^typeName--it denotes a type that represents
pointers to variables of type typeName. When it appears after a pointer variable
--pointer^--it dereferences the pointer; that is, it returns the value stored at the
memory address held by the pointer.Our example may seem like a round about way of
copying the value of one variable to another--something that we could have accomplished
with a simple assignment statement. But pointers are useful for several reasons.
First, understanding pointers will help you to understand the Delphi language, since
pointers often operate behind the scenes in code where they don‘t appear explicitly.
Any data type that requires large, dynamically allocated blocks of memory uses pointers.
Long-string variables, for instance, are implicitly pointers, as are class instance variables. Moreover, some advanced p
rogramming techniques require the use of pointers.Finally, pointers are sometimes the
only way to circumvent Delphi‘s strict data typing. By referencing a variable with an
all-purpose Pointer, casting the Pointer to a more specific type, and then dereferencing
it, you can treat the data stored by any variable as if it belonged to any type.
For example, the following code assigns data stored in a real variable to an integer variable.
type
  PInteger = ^Integer;
var
  R: Single;
  I: Integer;
  P: Pointer;
  PI: PInteger;
begin
  ...
  P := @R;
  PI := PInteger(P);
  I := PI^;
end;
  Of course, reals and integers are stored in different formats. This assignment
simply copies raw binary data from R to I, without converting it.In addition to
assigning the result of an @ operation, you can use several standard routines to
give a value to a pointer. The New and GetMem procedures assign a memory address
to an existing pointer, while the Addr and Ptr functions return a pointer to a
specified address or variable.Dereferenced pointers can be qualified and can function
as qualifiers, as in the expression P1^.Data^.The reserved word nil is aspecial
constant that can be assigned to any pointer. When nil is assigned to a pointer,
the pointer doesn‘t reference anything.

delphi中指针操作符^的使用,布布扣,bubuko.com

时间: 2024-10-15 02:59:37

delphi中指针操作符^的使用的相关文章

delphi中一切皆指针

unit Unit1; interface uses  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,  Dialogs, StdCtrls,TypInfo,ObjAuto; type  TForm1 = class(TForm)    btn1: TButton;    btn2: TButton;    btn3: TButton;    btn4: TButton;    btn5: TB

Delphi中TList类应用

在DELPHI中指针最常见的就是和类TLIST结合起来使用.下面是一个很简单的例子,希望对这个例子的分析能让大家对使用TLIST类有一个简单的认识. 代码的功能是使用指针和Tlist来生成一个牌串,并将牌串保存在t_CardInfo中. procedure TForm1.Button1Click(Sender: TObject); const //黑桃,红桃,方块,草花 CardType:array[0..3] of String = ('S','H','D','C'); const //取出的

Delphi中的指针类型

首先讲讲指针类型在delphi中是怎么定义的: 指针类型的定义语法 type <指针类型标识符>=^<基类型>: 指针指向动态变量的类型是由^符后的基类型来标识,^符号也就是指针类型,而C++中是用*符号来表示指针类型. 举例说明 type student = record    //定义个记录类型,与c++中的结构类型类似 name:string[8]; number:integer; sex:(b,g); age:integer; end; per = ^student;  /

Delphi对象的秘密:在Delphi中的类实例实际上是指向堆中的类实例数据的3 2位指针

在Delphi中的类实例实际上是指向堆中的类实例数据的32 位指针 当访问对象的域.方法和属性时,编译器会自动产生一些代码来处理这个指针.因此对于新手来说,对象就好像是一个静态变量.这意味着,Delphi无法像C++那样在应用程序的数据段中为类分配内存,而只能在堆中分配内存. 所以Delphi的对象实例需要最后自己调用Free()方法来释放,而不能在作用域结束之后被自动释放

Delphi中的函数指针判断是否为空

delphi函数指针 只有@@p才代表了函数指针本身的地址   assigned(p) 判断是否为空 或者用 @p=nil 来判断函数指针是不是为空 Delphi中的函数指针实际上就是指针,只是在使用的时候有些不同 函数指针要先定义一个函数类型,比如 type TTestProc = procedure of object; 这是一个最简单的函数类型,没有参数,也没有返回值,并且要求是类的成员函数 类的成员函数其实就代表了调用的时候参数的不同,因为类的成员函数隐含着一个对象参数,而不是显式写明,

Delphi中@,^,#,$分别表示什么?

var int:integer; p:^integer; new(P); int:=24; p:[email protected]; dispose(P); ^:指针的引用解析操作符; var pint:^integer; new(pint); showmessage(inttohex(integer(@pint),8)); pint^:=$ff; showmessage(inttohex(pint^,4)); #:ASCII码值表示符; const tab_key=#9;//TAB键的ASCI

初探Delphi中的插件编程

前言 我写Delphi程序是从MIS系统入门的,开始尝试子系统划分的时候采用的是MDI窗体的结构.随着系统功能的扩充,不断有新的子系统加入系统中,单个工程会变得非常大,每次做一点修改都要重新编译,单个工程的形式也不利于团队协作.为了提高工作效率,我希望利用DLL动态链接库的形式实现插件结构的编程. 插件结构的编程需要一个插件容器来控制各DLL的运行情况,将划分好的每个子系统安排到一个DLL库文件中.对每个DLL程序需要为容器预留接口函数,一般接口函数包括:启动调用DLL库的函数.关闭DLL库的函

Delphi中使代码简洁的 5 条忠告(转)

写代码是一种艺术.使用Delphi,任何人都可以轻而易举地开发出某种软件.完成某些任务.而完美的代码则只有真正的高手才能写出.除了正确的缩进.大小写.命名规则之外,请时刻牢记爱因斯坦的名言--简单就是美.下面将谈及的五个代码问题,可能是初学者.甚至一些老鸟都会犯的错误. 忠告一 布尔型变量的赋值操作应该是直接的.例如,在一个if/then/else语句中,if子句将布尔型变量赋值为True,而else子句将其赋为False.下面这段代码的写法是不好的: if If_Love_Delphi the

Delphi的指针(有图,很清楚)

Pointers are like jumps, leading wildly from one part of the data structure to another. Their introduction into high-level languages has been a step backwards from which we may never recover. — Anthony Hoare 对指针可能有最让人误解和惧怕的数据类型,因此很多程序员喜欢躲避他们. 但是指针很重要