博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
delphi中指针操作符^的使用
阅读量:5030 次
发布时间:2019-06-12

本文共 3201 字,大约阅读时间需要 10 分钟。

  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.

转载于:https://www.cnblogs.com/spiritofcloud/p/3898365.html

你可能感兴趣的文章
二进制集合枚举子集
查看>>
磁盘管理
查看>>
SAS学习经验总结分享:篇二—input语句
查看>>
UIImage与UIColor互转
查看>>
RotateAnimation详解
查看>>
系统管理玩玩Windows Azure
查看>>
c#匿名方法
查看>>
如何判断链表是否有环
查看>>
【小程序】缓存
查看>>
ssh无密码登陆屌丝指南
查看>>
MySQL锁之三:MySQL的共享锁与排它锁编码演示
查看>>
docker常用命令详解
查看>>
jQuery技巧大放送
查看>>
字符串转换成JSON的三种方式
查看>>
Hive时间函数笔记
查看>>
clojure-emacs-autocomplete
查看>>
一个自己写的判断2个相同对象的属性值差异的工具类
查看>>
10 华电内部文档搜索系统 search03
查看>>
[HIHO1149]回文字符序列(dp)
查看>>
[HDU1402]A * B Problem Plus(FFT)
查看>>