Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


Pascal....about array....

Aggyness_90Aggyness_90 Member Posts: 5
hi again.....I have a question about array in Pascal....
I wanted to know whenever I wanna use an array should I always know what exactly the lenght is??? I mean:

a:array[1..[b]n[/b]] of integer;
I wanna write 'n' instead of writing a number (e.g: 10 or 345 or else...) cause I want to find the lenght during the program...cause it may became different by different datas each time when I run the I don't wanna write a number as a lenght for my array....but I have a compiler problem in runing my program
the error is:(cannot evaluate this expression) what should I do then??


  • AtexAtex Member Posts: 268
    Turbo Pascal doesn't support dynamic arrays, so you must set your array to the expected maximum size and have a variable to keep count the actual size:[code][color=Blue]var a:array [1..500] of byte; { <-- reserved 500 bytes of memory }
    counter:byte; { <-- a variable to keep count the actual array size}[/color][/code]Of course your code have to be written accordingly[code][color=Blue]counter:=5; { <-- set array size to 5}
    for i:=1 to counter do
    a[i]:=0; { <-- fill array with 0's }

    counter:=40; { <-- increase array size to 40 }
    for i:=1 to counter do... {} [/color][/code]
    On the other hand if you're using Free Pascal you can have dynamic arrays by switching the compiler to Delphi mode: [b]{$mode delphi}[/b][code][color=Blue]type dynamic_array:array of byte;

    var a:dynamic_array;

    setlenght(a,500); { <-- reserve 500 bytes of memory }[/color][/code]Note that all dynamic array indexing are zero based, so valid indexes will be in range [0..499]. To create multidimensional arrays:[code][color=Blue]type dynamic_array_2d:array of array of byte;

    var b:dynamic_array_2d;

    setlength(b,5,5); {<-- same as: b:array[0..4,0..4] of byte; }[/color][/code]It is also possible to resize or copy dynamic arrays using the [b]copy[/b] procedure the same way as for strings...
Sign In or Register to comment.