2017-02-12 1 views
0

これは私が学校プロジェクトのために作った2つのリンクリストです。 2番目から最初のリストを呼びたいと思います。コンパイル時はすべてOKです。私がそれを実行すると、それは言う: プロジェクト(myProject)例外クラス '外部:SIGSEGV'を発生させた。アドレス40D32Dここ で は私のコードです:リンクリストへのパスカルリンクリストは機能しません

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. 

、エラーが誰も私を説明したり、私にどんな提案:)

答えて

4

を与える場合、私は非常に感謝するでしょうread(a[i]^.rreshti^.pozicioni); で、forループであります提供されたソースコードは、パスカルでのポインタ管理に関して少なくとも2つの誤解を示しています。

主な問題 - データを割り当てるには、recordタイプが前に割り当てられなければなりません。

この問題は、read(a[i]^.rreshti^.pozicioni);read(a[i]^.rreshti^.vlera);という行を参照しています。

a[i]rreshti両方がポインタ型(list=^pointer; & list2=^ptr;)として宣言され、データを割り当てる前に、レコード構造に割り当てられなければなりません。

ステップ1:ループにa[i]ポインタを割り当てます。

new(a[i]); 

ステップ2:ループ内a[i]^.rreshtiポインタを割り当てます。

new(a[i]^.rreshti); 

奇妙な問題からrecord型へのポインタを割り当て、宛先タイプを尊重しなければなりません。

この問題は、a[i]:[email protected];という行を参照しています。

a[i]list=^pointer;kjovlere:list2;ために宣言されていないlist2list2=^ptr;)でlistあります。

解決方法:その行を削除してください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のおかげで、あなたが行っているすべての説明のためにたくさん.. :) –

関連する問題