1
votes

These are two linked lists that I've made,for a school project... I want the first list to be called from the second,I have done that and at the compile time everything is ok. When I run it it says : Project (myProject) raised exception class 'External: SIGSEGV'. At address 40D32D Here is my code:

   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]:=@kjovlere;
        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.  

And the error is at the for loop,at the read(a[i]^.rreshti^.pozicioni); I would be very thankful if anyone explains me or gives me any suggestion :)

1

1 Answers

4
votes

The provided source code shows at least two misunderstandings about pointer management in Pascal.

Main Problem - To assign data, a record type shall be allocated before.

This problem is referring to the lines read(a[i]^.rreshti^.pozicioni); and read(a[i]^.rreshti^.vlera);.

Both a[i] and rreshti are declared as pointer type (list=^pointer; & list2=^ptr;) and shall be allocated to a record structure before assigning data.

Step1: allocate the a[i] pointer in the loop.

new(a[i]);

Step2: allocate the a[i]^.rreshti pointer in the loop.

new(a[i]^.rreshti);

Strange Problem - Assign a pointer to a record type shall respect the destination type.

This problem is referring to the line a[i]:=@kjovlere;.

The a[i] is a list which is list=^pointer; and not list2 (list2=^ptr;) as declared for kjovlere:list2;.

Solution is: remove that line a[i]:=@kjovlere;.

Solution:

begin
    for i:=1 to 7 do begin
        (* a[i]:=@kjovlere; 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.