Delphi 的类型与指针
Delphi 的指针分为 "类型指针" 和 "无类型指针" 两类.
Delphi 中的类型, 常用的也得有几百个, 我们可以给每种类型定义相应的类型指针.
其实 Delphi 已经为很多类型预定义了指针, 譬如数据类型:
Integer 有对应的 PInteger;
Char 有对应的 PChar;
string 有对应的 PString;
再譬如:
TPoint 有对应的 PPoint;
TColor 有对应的 PColor 等等.
另外, 指针也可以有指针, 譬如: PChar 是字符指针, PPChar 又是 PChar 的指针(这都是 Delphi 预定义的).
根据上面的例子, 咱们先总结一下类型与指针的命名规则:
类型约定用 T 打头(Delphi 常规的数据类型除外, 譬如: String);
指针约定用 P 打头;
指针的指针约定用 PP 打头.
类型和指针是不可分的两个概念, 指针本身也是一种类型 - "指针类型".
先认识一下指针相关的操作符(@、^、Addr):
@ | @变量 | 获取变量指针 |
---|---|---|
Addr | Addr(变量) | |
^ | 指针^ | 获取指针指向的实际数据 |
var Pxxx: ^类型 | 定义 Pxxx 某种类型的指针的变量 | |
type Pxxx = ^类型 | 定义 Pxxx 为某种类型的指针 |
举例说明:
unit Unit1; interface uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls; type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
end; var
Form1: TForm1; implementation {$R *.dfm} //Integer 与 PInteger
procedure TForm1.Button1Click(Sender: TObject);
var
int: Integer;
pint: PInteger; {定义类型指针, Integer 类型的指针}
begin
int := ;
pint := @int; {现在 pint 就是 int 的指针}
pint^ := pint^ + ; {现在 pint^ 和 int 是一回事, 测试一下:}
ShowMessage(IntToStr(int)); {101}
ShowMessage(IntToStr(pint^)); {101}
end; //直接定义类型指针
procedure TForm1.Button2Click(Sender: TObject);
var
int: Integer;
PMyInt: ^Integer;
begin
int := ;
PMyInt := Addr(int); {这句和: PMyInt := @int; 相同}
PMyInt^ := PMyInt^ + ;
ShowMessage(IntToStr(int)); {101}
ShowMessage(IntToStr(PMyInt^)); {101}
end; //先自定义指针类型
procedure TForm1.Button3Click(Sender: TObject);
type
PInt = ^Integer;
var
int: Integer;
PMyInt: PInt;
begin
int := ;
PMyInt := @int;
PMyInt^ := PMyInt^ + ;
ShowMessage(IntToStr(int)); {101}
ShowMessage(IntToStr(PMyInt^)); {101}
end; //指针的指针
procedure TForm1.Button4Click(Sender: TObject);
var
int: Integer;
pint: PInteger;
ppint: ^PInteger;
begin
int := ;
pint := @int;
ppint := @pint;
ppint^^ := ppint^^ + ;
ShowMessage(IntToStr(int)); {101}
ShowMessage(IntToStr(pint^)); {101}
ShowMessage(IntToStr(ppint^^)); {101}
end; end.
知道以上这些就可以操作了, 就可以看懂别人的代码了; 不过要想彻底明白指针到底是怎么回事, 需要从内存谈起.
在来学习下,通过创建线程CreateThread,通过指针传递参数到线程里面去;
function myFun(ID:PInteger):boolean;stdcall;
begin
form1.Memo1.Lines.Add('ID='+intTostr(ID^));
end; procedure TForm1.Button3Click(Sender: TObject);
var
ID : Pinteger;
begin
new(ID); //分配内存空间
ID^ := 2; //怎么把ID=2传到myFun里面去
CreateThread(nil, 0, @myFun, ID, 0, TID);
Dispose(ID);//释放内存空间
end;
Delphi 的类型与指针的更多相关文章
- Delphi 记录类型- 结构指针
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...
- delphi 数组类型与数组指针的巧妙利用
{本例通过存取结构, 慢慢引入了数组类型与指针的一些使用方法; 其中六个小例子的测试内容和结果都是一样的. ---------------------------------------------- ...
- Delphi中的函数指针判断是否为空
delphi函数指针 只有@@p才代表了函数指针本身的地址 assigned(p) 判断是否为空 或者用 @p=nil 来判断函数指针是不是为空 Delphi中的函数指针实际上就是指针,只是在使用 ...
- delphi中一切皆指针
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Form ...
- 不同类型的指针+1之后增加的大小不同(a,&a的地址是一样的,但意思不一样)
main() { ]={,,,,}; ); printf(),*(ptr-)); } *(a+1)就是a[1],*(ptr-1)就是a[4], 执行结果是2, 5.&a+1不是首地址+1,系统 ...
- C++:不同类型的指针的本质与差异
转自:http://blog.csdn.net/richerg85/article/details/10076365 指针的类型(The Type of a Pointer) 一 ...
- 直接修改托管堆栈中的type object pointer(类型对象指针)
都知道.NET是一个强对象类型的框架. 那么对于对象类型又是怎么确定的呢. 最初的我简单认为数据的类型就是定义时字段的类型修饰决定的(回来发现这种观点是绝对错误的) 我们知道引用对象存储在托管堆栈中, ...
- 为什么C++类定义中,数据成员不能被指定为自身类型,但可以是指向自身类型的指针或引用?为什么在类体内可以定义将静态成员声明为其所属类的类型呢 ?
static的成员变量,不是存储在Bar实例之中的,因而不会有递归定义的问题. 类声明: class Screen: //Screen类的声明 1 类定义: class Screen{ //Scree ...
- C# CLR via 对象内存中堆的存储【类型对象指针、同步块索引】
最近在看书,看到了对象在内存中的存储方式. 讲到了对象存储在内存堆中,分配的空间除了类型对象的成员所需的内存量,还有额外的成员(类型对象指针. 同步块索引 ),看到这个我就有点不懂了,不知道类型对象指 ...
随机推荐
- SELECT * INTO xx FROM x0
insert into a select * from b:--向存在表中插入数据,如果不存在表a报错. select * into a from b:--创建新表的同时插入数据,如果表a存在,报错. ...
- 关于CDN对动态网站加速的一些看法
CDN的全称是Content Delivery Network,即内容分发网络.其目的是通过在现有的Internet中增加一层新的网络架构,将网站的内容发布到最接近用户的网络"边缘" ...
- 网络数据传输socket和http优缺点
数据传输方式 Socket传输的定义 所谓socket通常也称作"套接字",实现服务器和客户端之间的物理连接,并进行数据传输,主要有UDP和TCP两个协议.Socket处于网络协议 ...
- 配置LANMP环境(10)-- 安装memcached与redis
一.安装memcached 1.安装 yum install memcached 2.设置1G内存 memcached -u memcached -p -m -c -d 3.启动与设置开机自启动 sy ...
- java后端技术
技术概论:Springmvc+mybatis+shiro+Dubbo+ZooKeeper+Redis+KafKa j2ee分布式架构 我在恒生工作,主要开发金融互联网第三方平台的对接项目.目前已经对接 ...
- TBSchedule源码阅读1-TBScheduleManagerFactory
TBSchedule 1 TBScheduleManagerFactory 初始化 成员变量 ZKManager; IScheduleDataManager; Schedule ...
- git add -A使用说明
git help add -A, --all Like -u, but match <filepattern> against files in the workin ...
- windows下XAMPP安装php_memcache扩展
windows下XAMPP安装php_memcache扩展 首先下载phpmemcache,地址为: http://up.2cto.com/2012/0522/20120522094758371.ra ...
- Django模板系统(非常详细)(后台数据如何展示在前台)
前面的章节我们看到如何在视图中返回HTML,但是HTML是硬编码在Python代码中的这会导致几个问题:1,显然,任何页面的改动会牵扯到Python代码的改动网站的设计改动会比Python代码改动更频 ...
- centos7.0 关闭防火墙
1.关闭firewall:systemctl stop firewalld.service #停止firewallsystemctl disable firewalld.service #禁止fire ...