0

I am still a beginner, so please forgive me if it is a stupid mistake or something. I want to write a program to generate prime numbers from 2 to n, and n is user-defined. Since I do not know n at the start of the program, I want to construct a dynamic array and setlength(n) afterwards. Here is a snippet of my code:

    program D401;
    type
       arr = array of int64;
    var
       x : int64;
       a : arr;
    begin
        readln(x);
        setlength(a, x);
    end.

But it won't work and and it says: Fatal: Syntax error, [ expected but OF found

I also tried this:

    program D401;
    var
       x : int64;
       a : array of int64;
    begin
        readln(x);
        setlength(a, x);
    end.

But it also produces the same error. I also used freepascal and GNU pascal but it also doesn't work. Is it dev-pascal's problem or it is not updated or something?

Thanks in advance.

6
  • Your programs look valid. What version of fpc is used? Commented Dec 22, 2016 at 5:23
  • Are you using a current FreePascal at all? If this syntax is not accepted, you are probably using a very old FreePascal or some other dialect of Pascal. Commented Dec 22, 2016 at 7:35
  • I could imagine that GNU Pascal doesn't accept the syntax. As far as I know, built-in dynamic arrays are a Delphi and FreePascal thing only. Commented Dec 22, 2016 at 7:37
  • I removed the freepascal tag. The question asks about devpascal. Commented Dec 22, 2016 at 7:54
  • @David Dev-Pascal is just IDE that uses freepascal (fpc) or gpc compilers, so error reason is related to compiler Commented Dec 22, 2016 at 9:27

2 Answers 2

1

Dev Pascal is ancient and uses old compilers that do not support dynamic array syntax. Simply put you should not use it today.

If you want a free development environment using an up-to-date Pascal compiler the best option is Lazarus, using a modern version of freepascal.

Sign up to request clarification or add additional context in comments.

2 Comments

I checked and indeed it doesn't support dyn arrays it seems. Strange, I thought that was already there in 1.1.x times. So best throw this back to 2004 and use a version from this decennium
Thanks Marco. Throw back to 2004? Most definitely the best option. Season's greetings to you by the way!
0

It doesn't matter in the modern world. But I am writing my answer for those who want to deliberately use the outdated GNU Pascal. I know a little about GNU Pascal but suggest a pointer based approach:

program dynarr;

type
  TElem = integer;
  TPArray = ^TElem;

var
  pArray: TPArray;
  length, i: INTEGER;

begin
  length := 10;
  GetMem(pArray, length * sizeof(TElem));

  for i := 0 to length-1 do
    (pArray+i)^:=i;
  for i := 0 to length-1 do
    writeln((pArray+i)^);

  FreeMem (pArray, length * sizeof(TElem));
  readln;
end.

Compile with --pointer-arithmetic option

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.