Its something really basic, but for some reason i cant get my dinamic array to work. Keep getting a message that i need to add the "[]", but then i would create a static array, not a dinamic one. The code:

program Noname0;
var arr : array of integer;
begin
  WriteLn('Hi!');
  Readln;
end.

Recommended Answers

All 3 Replies

I copy pasted your code in empty project in Lazarus and it run without error:

program Project1;

{$mode objfpc}{$H+}

uses
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}
  Classes
  { you can add units after this };

{$R *.res}
var arr : array of integer;
begin
  WriteLn('Hi!');
  Readln;
end.

Also I run as more advanced code some code from: http://delphi.about.com/od/beginners/a/arrays.htm .

program dynamic;

{$mode objfpc}{$H+}

uses
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}
  Classes
  { you can add units after this };

{$R *.res}
var
 Vector: array of Integer;
 k : integer;
begin
     SetLength(Vector, 10) ;
     for k := Low(Vector) to High(Vector) do
         Vector[k] := k*10;
     //...
     //now we need more space
     SetLength(Vector, 20) ;
     Vector[11] := 10;
     for k := Low(Vector) to High(Vector) do writeln(k, ', ', Vector[k]);
     //here, Vector array can hold up to 20 elements
     //(it already has 10 of them)
     readln;
end.

I know it should work with no errors, dont know where the problem is.

What version of Delphi/Object Pascal are you using and with what compiler flags/directives?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.