### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

### Author Topic: Multidimensional Array Question  (Read 1032 times)

• Jr. Member
• Posts: 91
##### 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 »
Wind XP / 7 / 10

#### jamie

• Hero Member
• Posts: 3648
##### 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.

The only true wisdom is knowing you know nothing

#### 440bx

• Hero Member
• Posts: 2011
##### 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].
FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

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

#### lucamar

• Hero Member
• Posts: 3075
##### 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 !!!)
Lazarus 2.0.8/FPC 3.0.4 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.