In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
In this issue, the editor will bring you about how to make clever use of array types and array pointers. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.
Through the access structure, some methods of using array types and pointers are slowly introduced; the test contents and results of six small examples are the same.
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
Button5: TButton
Button6: TButton
Procedure Button1Click (Sender: TObject)
Procedure Button2Click (Sender: TObject)
Procedure Button3Click (Sender: TObject)
Procedure Button4Click (Sender: TObject)
Procedure Button5Click (Sender: TObject)
Procedure Button6Click (Sender: TObject)
End
Var
Form1: TForm1
Implementation
{$R * .dfm}
Type {define structure and structure pointer first}
PMyRec = ^ TMyRec
TMyRec = record
F1: Char
F2: Word
End
{it is convenient to store or read / write structures in static arrays.
Procedure TForm1.Button1Click (Sender: TObject)
Var
ArrRec: array [0..2] of TMyRec
I: Integer
Begin
{write}
For I: = 0 to 2 do
Begin
ArrRec.F1: = Chr (iTun65)
ArrRec.F2: = (iTun1) * 10
End
{read}
For I: = 0 to Length (ArrRec)-1 do
ShowMessageFmt ('% s,% dwells, [ArrRR [I] .F1, ArrRR [I] .F2])
End
{this only defines a piece of memory, does not use the concept of array, it is a bit troublesome.
Procedure TForm1.Button2Click (Sender: TObject)
Var
Buf: PMyRec
Begin
GetMem (buf, SizeOf (TMyRec) * 3); {request memory}
{write}
Buf ^ .F1: ='A'
Buf ^ .F2: = 10
Inc (buf); {to next}
Buf ^ .F1: ='B'
Buf ^ .F2: = 20
Inc (buf)
Buf ^ .F1: ='C'
Buf ^ .F2: = 30
Dec (buf, 2); {back to start}
{read}
ShowMessageFmt ('% s,% dumped, [BUF ^ .F1, BUF ^ .F2])
Inc (buf); {to next}
ShowMessageFmt ('% s,% dumped, [BUF ^ .F1, BUF ^ .F2])
Inc (buf)
ShowMessageFmt ('% s,% dumped, [BUF ^ .F1, BUF ^ .F2])
Dec (buf, 2); {back to start}
FreeMem (buf); {release memory}
End
{you can define an array type for the structure}
Procedure TForm1.Button3Click (Sender: TObject)
Type
TArr = array of TMyRec
Var
Buf: PMyRec
I: Integer
Begin
GetMem (buf, SizeOf (TMyRec) * 3)
For I: = 0 to 2 do
Begin
TArr (buf) [I] .F1: = Chr (iTun65)
TArr (buf) [I] .F2: = (iTun1) * 10
End
For I: = 0 to 2 do
ShowMessageFmt ('% s,% dwells, [TArr (buf) [I] .F1, TArr (buf) [I] .F2])
FreeMem (buf)
End
{it is OK to use dynamic array directly.
Procedure TForm1.Button4Click (Sender: TObject)
Var
Arr: array of TMyRec
I: Integer
Begin
SetLength (Arr, 3)
For I: = 0 to 2 do
Begin
Arr [I] .F1: = Chr (iTun65)
Arr [I] .F2: = (iTun1) * 10
End
For I: = 0 to 2 do
ShowMessageFmt ('% s,% dwells, [Arr [I] .F1, Arr [I] .F2])
End
{use an array pointer to an element, which is very common}
Procedure TForm1.Button5Click (Sender: TObject)
Type
PArr = ^ TArr
TArr = array [0.. 0] of TMyRec
Var
Buf: PArr
I: Integer
Begin
GetMem (buf, SizeOf (TMyRec) * 3)
For I: = 0 to 2 do
Begin
BUF ^ [I] .F1: = Chr (iTun65)
Buf ^ [I] .F2: = (iTun1) * 10
End
{you can't read or write in this way, that is, a constant in [] cannot be more than 0, but you can use a variable}.
{this is also easy to understand, because constants are compiled directly into the code, and the compiler does not know that the array will be larger until memory is allocated.
{the next method is needed to solve this problem.
/ / buf [0] .F1: ='A'
/ / buf [0] .F2: = 10
/ / buf [1] .F1: ='B'
/ / buf [1] .F2: = 20
/ / buf [2] .F1: ='C'
/ / buf [2] .F2: = 30
For I: = 0 to 2 do
ShowMessageFmt ('% s,% dumped, [buf [I] .F1, BUF [I] .F2])
FreeMem (buf)
End
{use a very large array pointer, as Delphi's TList class does}
Procedure TForm1.Button6Click (Sender: TObject)
Type
PArr = ^ TArr
TArr = array [0.100000] of TMyRec; {Don't worry about the memory explosion, we only use its pointer when using it.
Var
Buf: PArr
I: Integer
Begin
GetMem (buf, SizeOf (TMyRec) * 3)
For I: = 0 to 2 do
Begin
BUF ^ [I] .F1: = Chr (iTun65)
Buf ^ [I] .F2: = (iTun1) * 10
End
{unlike the previous example, the following code can also}
/ / buf [0] .F1: ='A'
/ / buf [0] .F2: = 10
/ / buf [1] .F1: ='B'
/ / buf [1] .F2: = 20
/ / buf [2] .F1: ='C'
/ / buf [2] .F2: = 30
For I: = 0 to 2 do
ShowMessageFmt ('% s,% dumped, [buf [I] .F1, BUF [I] .F2])
FreeMem (buf)
End
End.
The above is the clever use of array types and array pointers shared by the editor. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.