2

For the sake of fun, I'd like to refresh my Pascal knowledge, so gotta review the basics again.

Let's see this Java code:

class ArrayTest {

    public static void main(String args[]){
        int[] numArray = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    }
}

Rewrite that into Pascal:

type
    arrNum = array[1..10] of integer;
var
    BBB: arrNum;

begin
    BBB := [1,2,3,4,5,6,7,8,8,10];
end.

Compiling the code with FPC 3.2.2 on MacOS, the result is:

arrnum.pas(7,12) Error: Incompatible types: got "{Array Of Const/Constant Open} Array > of ShortInt" expected "arrNum"

arrnum.pas(10) Fatal: There were 1 errors compiling module, stopping

Fatal: Compilation aborted Error: /usr/local/bin/ppcx64 returned an error exitcode

What's wrong here?

1
  • Try declaring arrNum as TArray<Integer> instead. Commented Apr 11, 2022 at 16:12

6 Answers 6

3

A similar syntax using a constructor is available for dynamic arrays (BBB), but not for static ones. Static initializations can be done by assigning a type constant to a variable (BB10:=CCC10) or by defining an initialized constant(DDD10).

The below code demonstrates some cases:

{$mode delphi} 
type
    arrNum = array of integer;
    arrNum10 = array[0..9] of integer;

const CCC10 : arrNum10 = (1,2,3,4,5,6,7,8,9,10);
var
    BBB: arrNum;
    BBB10 : arrNum10;


    DDD10  : arrnum10 = (1,2,3,4,5,6,7,8,9,10);

begin
    BBB := ArrNum.Create(1,2,3,4,5,6,7,8,8,10);
    BBB10:=CCC10;
end.
Sign up to request clarification or add additional context in comments.

Comments

2

You can initialize it in the declaration:

program ideone;
type
    arrNum = array[1..10] of integer;
var
    BBB: arrNum = (1,2,3,4,5,6,7,8,8,10);
    i: Integer;
begin
    for i := 1 to 10 do
        Write(BBB[i])
end.

Comments

2

In Pascal [1, 2, 3, 4, 5, 6, 7, 8, 8, 10] is a set literal containing 9 distinct integer values. You cannot assign sets to an array data type variable.

Instead you will need to specify an array literal and properly denote it, like so:

arrNum[1: 1; 2: 2; 3: 3; 4: 4; 5: 5; 6: 6; 7: 7; 8: 8; 9: 8; 10: 10]

However, as of version 3.2.0 the FPC does not (yet) comply with the Extended Pascal specification (ISO standard 10206). They brewed up their own Pascal, so the compiler (“mis‑”)identifies your set literal as an “{Array Of Const/Constant Open} Array of ShortInt”. See Marco’s answer how to initialize “dynamic arrays” as an alternative.

Comments

1

Using a dynamic array and delphi compatibility mode, it is possible to assign an array using this syntax:

{$mode delphi}

type
  arrNum = TArray<integer>;
var
  BBB: arrNum;    
begin
  BBB := [1,2,3,4,5,6,7,8,8,10];
end.

If you want a static constant (dynamic) array, the assignment is similar:

const
  BBB : arrNum = [1,2,3,4,5,6,7,8,8,10];

Comments

0

If you declare as an array you should use a for loop to init

type
    arrNum = array [ 1..10] of integer;
var
    BBB: arrNum;
    i: integer;

begin
    for i:=1 to 10 do
    BBB[i] := i;
end.

Comments

0

You can initialize it using for loop

for i := 1 to 10 do BBB[i]:=i;

or

for i := 1 to 10 do BBB[i]:=0;

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.