delphi - Invalid Pointer Operation with Dynamic Array -
heloo guys... once try run code gettin 'invalid pointer operation' error, problem ? supposed sort names in textfile alphabetical order (school project).
program project2; {$apptype console} uses sysutils, classes; var names : textfile; count : integer=0; array : array of string; : integer; procedure load; begin reset(names); setlength(array, count - 1); := 1 count readln(names, array[i]); end; begin assignfile(names, 'names.txt'); reset(names); while not eof(names) begin readln(names); inc(count); end; load; := 1 count writeln(array[i]); readln; closefile(names); erase(names); end.
dynamic arrays zero-based. need set length count
, iterate 0
count-1
.
setlength(arr, count); := 0 count-1 readln(names, arr[i]);
obviously indexing needs changed 0-based.
i changed name of array arr
because array
keyword.
do not attempt continue 1-based indexing. doing cause endless headaches.
also should enable range checking compiler option. doing yield informative errors @ runtime if access array out of bounds.