2017-02-12 106 views
0

這些是我爲一個學校項目製作的兩個鏈接列表... 我希望從第二個列表中調用第一個列表,我已經完成了這個任務並在編譯時間一切都很好。當我跑它說: 項目(myProject)引發異常類'外部:SIGSEGV'。 在地址40D32D 這裏是我的代碼:Pascal鏈接列表鏈接列表不起作用

list2=^ptr; 
    ptr=record 
     vlera:integer; 
     pozicioni:integer; 
    end; 

type 
    list=^pointer; 
    pointer=record 
     rreshti:list2; 
    end; 
    var 
    a:array[1..7] of list; 
    i:integer; 
    kjovlere:list2; 

begin 
    for i:=1 to 7 do begin 
     a[i]:[email protected]; 
     write('Give the pozition for the row:',i,' : '); 
     read(a[i]^.rreshti^.pozicioni); 
     write ('give the value for this poziton :'); 
     read(a[i]^.rreshti^.vlera); 
     writeln; 
    end; 
end. 

和錯誤是在for循環,在read(a[i]^.rreshti^.pozicioni); 如果有人介紹我,也不給我任何建議:)

回答

4

我會很感激提供的源代碼顯示了至少兩個關於Pascal中指針管理的誤解。

主要問題 - 要分配的數據,一個record類型應該被分配之前。

此問題指的是行read(a[i]^.rreshti^.pozicioni);read(a[i]^.rreshti^.vlera);

兩個a[i]rreshti被聲明爲指針類型(list=^pointer; & list2=^ptr;)和分配數據之前應分配給一個記錄結構。

第1步:在循環中分配a[i]指針。

new(a[i]); 

第二步:分配迴路中a[i]^.rreshti指針。

new(a[i]^.rreshti); 

奇怪問題 - 分配一個指向record類型應遵守目標類型。

這個問題指的是行a[i]:[email protected];

a[i]listlist=^pointer;和申報kjovlere:list2;list2list2=^ptr;)。

解決方法是:刪除該行a[i]:[email protected];

解決方案:

begin 
    for i:=1 to 7 do begin 
     (* a[i]:[email protected]; to be removed *) 
     new(a[i]); 
     new(a[i]^.rreshti); 
     write('Give the pozition for the row:',i,' : '); 
     read(a[i]^.rreshti^.pozicioni); 
     write ('give the value for this poziton :'); 
     read(a[i]^.rreshti^.vlera); 
     writeln; 
    end; 
end. 
+0

J. Piquard非常感謝,對所有你所做的解釋.. :) –