Recent

Author Topic: Error with a 2D array  (Read 2397 times)

TJ11235

  • Newbie
  • Posts: 1
Error with a 2D array
« on: March 16, 2019, 06:01:08 pm »
I have Cards:array[1..HalfSize][1..2] of string; where HalfSize is an integer and is not bringing up an error but when passing it into a procedure the error comes telling me that it was expecting "OF" got "[" after the word array. This problem doesn't occur when I have Cards:TCards; where Type TCards = array[1..HalfSize][1..2] of string;. However when using TCards I define it after obtaining a value for Halfsize so it is defined in a procedure and is not global.
« Last Edit: March 16, 2019, 06:09:46 pm by TJ11235 »

Handoko

  • Hero Member
  • *****
  • Posts: 5130
  • My goal: build my own game engine using Lazarus
Re: Error with a 2D array
« Reply #1 on: March 16, 2019, 06:22:54 pm »
Hello TJ11235,
Welcome to the forum.

Maybe what you need is open array:
https://www.freepascal.org/docs-html/ref/refsu68.html

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Error with a 2D array
« Reply #2 on: March 16, 2019, 06:24:16 pm »
Please show where and how you're obtaining the value for Halfsize and how you're passing the array to the procedure (i.e. what's the procedure signature). If you can, add the code you're actually using so we can see what you're trying to do.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Handoko

  • Hero Member
  • *****
  • Posts: 5130
  • My goal: build my own game engine using Lazarus
Re: Error with a 2D array
« Reply #3 on: March 16, 2019, 06:31:48 pm »
I could be wrong. But if you declare a type, for example:

Quote
Type
  MyType = array[0..AValue] of Integer;

The "AValue" must be a constant. It cannot be a variable, can it?

OP said:
"... TCards I define it after obtaining a value for Halfsize"

That means the Halfsize (in my example "AValue") is a variable.

---edit---

I just tested using Lazarus 1.8.4 this code fails to compile:

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, Forms;
  9.  
  10. type
  11.   TForm1 = class(TForm)
  12.   private
  13.  
  14.   public
  15.  
  16.   end;
  17.  
  18. var
  19.   Form1: TForm1;
  20.   AValue: Integer;
  21.  
  22. Type
  23.   MyType = array[0..AValue] of Integer;
  24.  
  25. implementation
  26.  
  27. {$R *.lfm}
  28.  
  29. end.

The error message was:
unit1.pas(23,27) Error: Can't evaluate constant expression

It is clear you cannot declare a new array type using a variable as its length.
« Last Edit: March 16, 2019, 07:25:25 pm by Handoko »

Kays

  • Hero Member
  • *****
  • Posts: 569
  • Whasup!?
    • KaiBurghardt.de
Re: Error with a 2D array
« Reply #4 on: March 16, 2019, 06:39:23 pm »
[…]Cards:array[1..HalfSize][1..2] of string; […]
Only comma separating dimensions is allowed during declaration/definition of array types, hence it must be
Code: Pascal  [Select][+][-]
  1. array[1..halfSize, 1..2] of string
Only when retrieving/assigning values to elements indices may be placed in dedicated square brackets.
Yours Sincerely
Kai Burghardt

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Error with a 2D array
« Reply #5 on: March 16, 2019, 07:58:39 pm »
I have Cards:array[1..HalfSize][1..2] of string; where HalfSize is an integer ...
If HalfSize is an integer variable (as opposed to a constant declared earlier) then you clearly want a dynamic array.
For this the syntax is straightforward, and a correctly declared type is readily used as a parameter in routines. For example:
Code: Pascal  [Select][+][-]
  1. type
  2.  
  3.   TStringArray = array of String;
  4.   TCards = array[1..2] of TStringArray;
  5.  
  6. var
  7.   cards: TCards;
  8.  
  9. procedure SetArraySize(firstDimension: Integer; aCardsArray: TCards);
  10. begin
  11.   SetLength(aCardsArray[1], firstDimension);
  12.   SetLength(aCardsArray[2], firstDimension);
  13. end;
Subsequently, if you had an array named Cards of type TCards you would call
Code: Pascal  [Select][+][-]
  1.   SetArraySize(HalfSize, Cards);
to adjust your variable-sized Cards array as you want  to the dimension of whatever the variable HalfSize contains.

In the days of TurboPascal there was a hack using the
Code: Pascal  [Select][+][-]
  1.  TCards = array[0..0] of String;

 syntax coupled with GetMem and FreeMem to contrive a dynamic array. However, none of that convoluted stuff is needed since FPC has had reliable dynamic arrays for many years now.
« Last Edit: March 16, 2019, 08:04:43 pm by howardpc »

 

TinyPortal © 2005-2018