全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:885
推到 Plurk!
推到 Facebook!

如果給定指標一個陣列的空間

尚未結案
etboite
一般會員


發表:10
回覆:17
積分:5
註冊:2003-02-17

發送簡訊給我
#1 引用回覆 回覆 發表時間:2003-06-02 16:23:00 IP:211.23.xxx.xxx 未訂閱
下面是我宣告的一個結構,我的用意是想說 IDlist 指向一個自訂 Buffer 的空間,而 len 為宣告的 Buffer 的個數,以目前下面的做法是可行的,可是要使用這個結構的時候我是用 WordList 下去做指定及變化,這樣的寫法我覺得有點奇怪,可否有另一種寫法,還是說如何指定 device 的 IDlist ? type DataList = packed record IDlist: ^Word; len: Integer; end; procedure Test() var WordList : array[0..64] of Word; device : DataList; i : Integer; begin device.DCIDlist := @WordList; device.len := 64; for i := 0 to 64 do List.Lines.Add(IntToStr(WordList[i])); end;
william
版主


發表:66
回覆:2535
積分:3048
註冊:2002-07-11

發送簡訊給我
#2 引用回覆 回覆 發表時間:2003-06-02 16:34:47 IP:147.8.xxx.xxx 未訂閱
I don't think this is 奇怪. Anyway I think the length of you WordList array is 65 not 64. Consider this kind of usage:    
type
    PWordList = ^TWordList;
    TWordList = array[0..9999] of Word;
{...}
var
    PW: PWordList;
    device : DataList; 
begin
    GetMem(PW,64*Size(word));
    device.IDlist := PW;
    device.len := 64;    
    {....}
    FreeMem(PW);
end;
etboite
一般會員


發表:10
回覆:17
積分:5
註冊:2003-02-17

發送簡訊給我
#3 引用回覆 回覆 發表時間:2003-06-02 17:45:16 IP:211.23.xxx.xxx 未訂閱
我有試過你的解答,但是程式沒有辦法 compiler 成功,會出現 Incompatiable types : 'TWordList' and 'Word'。是不是 device.IDlist 它的型態為 Word 而不是 TWordLIst ,所以無法相容?還有我覺得疑惑的地方是,如果照這樣的宣告做法,是不是會變成多分配一次的空間?    我根據你所提供的資訊,寫成下列這樣,這樣在 compiler 的過程是可以過的,可是取出來的值並不是正確的,是否有哪裡出問題呢?    GetMem(device.IDlist, 64*SizeOf(word)); ren = myfunc(device); <-這邊會將一些值回傳到 device 裡面 for i := 0 to 63 do begin   List.Lines.Add(IntToStr(device.IDlist^));   inc(device.IDlist, SizeOf(Word)); end; FreeMem(device.IDlist);    
引言: I don't think this is 奇怪. Anyway I think the length of you WordList array is 65 not 64. Consider this kind of usage:
type
    PWordList = ^TWordList;
    TWordList = array[0..9999] of Word;
{...}
var
    PW: PWordList;
    device : DataList; 
begin
    GetMem(PW,64*Size(word));
    device.IDlist := PW;
    device.len := 64;    
    {....}
    FreeMem(PW);
end;
william
版主


發表:66
回覆:2535
積分:3048
註冊:2002-07-11

發送簡訊給我
#4 引用回覆 回覆 發表時間:2003-06-03 10:55:01 IP:147.8.xxx.xxx 未訂閱
I think you can typcast it to pointer or change your definition of DataList. But what exactly is the function myfunc?
系統時間:2024-05-18 21:37:06
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!