Recent

Author Topic: Multidimensional Array Question  (Read 728 times)

coradi

  • Jr. Member
  • **
  • Posts: 90
Multidimensional Array Question
« on: October 21, 2019, 11:03:08 pm »
Hi,
an array [1..4,1..4] is clear
Code: Pascal  [Select]
  1.                       oooo
  2.                       oooo
  3.                       oooo
  4.                       oooo

But what is with this [1990..1992,1..2]
                 
Code: Pascal  [Select]
  1.                      1990  x
  2.                      1991  x
  3.                      1992  x
  4.  

OR is it
Code: Pascal  [Select]
  1.                      1990  x    x
  2.                      1991  x    x
  3.                      1992  x    x
  4.  
« Last Edit: October 21, 2019, 11:09:00 pm by coradi »
Amstrad Schneider CPC 6128
Wind XP / 7 / 10

jamie

  • Hero Member
  • *****
  • Posts: 2068
Re: Multidimensional Array Question
« Reply #1 on: October 21, 2019, 11:18:24 pm »
You can define a range of indexes into an array, they do not need to start at 1. But the compiler will do a range test if you have range check turned on.

In this example 1990, 1991 and 1992 is the same as saying [0..2,....

The difference is, when you index the array in code you need to use a starting value of 1990 through 1992 to stay in range.

 In the background in code the compiler knows the starting number is 1990 so it subtracts that from what ever index value you have to give it, to obtain 0 for the starting index.

 So care must be taken to ensure you are within range. >:(

Number 1 at blue screen app creations!

440bx

  • Hero Member
  • *****
  • Posts: 1189
Re: Multidimensional Array Question
« Reply #2 on: October 21, 2019, 11:22:33 pm »
But what is with this [1990..1992,1..2]
Code: Pascal  [Select]
  1.                      1990  x    x
  2.                      1991  x    x
  3.                      1992  x    x
  4.  
That one.

When in doubt about things like that, just write a little program that outputs the address of the array elements, e.g, @TheArray[i, j, k, .... z]. 
using FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

coradi

  • Jr. Member
  • **
  • Posts: 90
Re: Multidimensional Array Question
« Reply #3 on: October 22, 2019, 08:23:24 am »
ah ok:-) Thanks. I will try it today
Amstrad Schneider CPC 6128
Wind XP / 7 / 10

lucamar

  • Hero Member
  • *****
  • Posts: 2075
Re: Multidimensional Array Question
« Reply #4 on: October 22, 2019, 12:33:42 pm »
For this cases it's good to remember that the alternative way to write: array [1990..1992,1..2] is: array[1990..1992] of array[1..2]. It makes clearer what is the disposition of the elements.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.2/2.0.4  - FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.