Delphi2005學習筆記3 陣列引數的研究

2021-03-31 08:56:58 字數 3072 閱讀 4983

有如下**:

procedure change(a:array of int32);

procedure change0(var a:array of int32);

type

ta = array[0..10] of int32;

procedure change2(a:ta);

procedure change3(var a:ta);

varaa:ta;

bb:array of int32;

implementation

procedure change(a:array of int32);

begin

a[0]:=123;

a:=bb;

end;

procedure change0(var a:array of int32);

begin

a[0]:=123;

a:=bb;

end;

procedure change2(a:ta);

begin

a[0]:=123;

a:=bb;

end;

procedure change3(var a:ta);

begin

a[0]:=123;

a:=bb;

end;

然後執行下面的語句

setlength(bb,11);bb[0]:=5678;

aa := ta(&array.createinstance(typeof(int32),11));

aa[0]:=0;

change(aa);

messagebox.show(aa[0].tostring());

if &object(aa)=&object(bb) then messagebox.show('=');

aa := ta(&array.createinstance(typeof(int32),11));

aa[0]:=0;

change0(aa);

messagebox.show(aa[0].tostring());

if &object(aa)=&object(bb) then messagebox.show('=');

aa := ta(&array.createinstance(typeof(int32),11));

aa[0]:=0;

change2(aa);

messagebox.show(aa[0].tostring());

if &object(aa)=&object(bb) then messagebox.show('=');

aa := ta(&array.createinstance(typeof(int32),11));

aa[0]:=0;

change3(aa);

messagebox.show(aa[0].tostring());

if &object(aa)=&object(bb) then messagebox.show('=');

結果發現 array of int32 方式,可以改變陣列元素的值,但不能改變陣列變數中儲存的陣列首位址,輸出123

var  array of int32 既可以改變陣列的值,又可以改變陣列變數中儲存的陣列首位址,輸出5678和=

ta方式,不能改變陣列元素的值,但是卻很奇怪,aa和bb指向同乙個陣列,輸出0和=

var ta方式 可以改變陣列元素的值,但是卻不是5678而是123;但是aa和bb指向同乙個陣列,輸出123和=

反彙編結果如下

method

public

static

void

change(int32

a)cil

managed

.method

public

static

void

change0(int32

& a)

cilmanaged

.method

public

static

void

change2(int32

a)cil

managed

.method

public

static

void

change3(int32

& a)

cilmanaged

結論:使用array of int32 方式,實際上對應於c#的可變數目引數,即 params int32,

這種引數方式是傳遞的陣列的首位址(即引數的值),而不是存放陣列首位址的變數的位址

加上var修飾,即加入ref修飾,傳遞的是存放陣列首位址的變數的位址(即引數變數自身的位址)

使用ta方式,則在函式內部對陣列的值進行轉殖,既不改變陣列的首位址,也不改變原陣列的值

加入var 修飾,好像傳遞的是變數自身的位址,這裡還是沒看懂是為什麼。

對應的c#**為

public

static

void

change(params

int a)

public

static

void

change0(params

refint

a)

public

static

void

change2(int

a) array

.copy

(winform

.bb, a

, num1

);}

public

static

void

change3(ref

int a)

array

.copy

(winform

.bb, a

, num1

);}

delphi2005 啟動加快

一 對於我們這種只要win32下面開發delphi的人 的c 和.的delphi都是多餘的 重新執行第一張cd的install 選擇modify 然後不選擇上面 delphi2005 for 和下面 c builder for 2個選項 只要中間 delphi2005 for win32 的就好了。...

delphi學習筆記 TADOQuery

delphi 資料庫查詢 tadoquery 在乙個程式中你會多次查詢資料庫的,因此在你寫的查詢方法中定義乙個區域性的adoquery就可以了,它返回乙個資料集 recordset。最簡單的應用如下 function getdata recordset 獲取一張表中的全部資料 recordset是一...

Delphi學習筆記四 語句

今天我們來看一下delphi的語句。一 常量宣告語句 和其他語言一樣,常量在宣告時就被賦值,且在程式執行過程中是不可改變的。常量用 表示兩邊的值是相等的。delphi view plain copy const pi 3.14159 answer 342 productname delphi 二 賦...