Reputation: 366
I’m perfectly avare that pointer math in Delphi should be used with special care, but I still don’t get where am I wrong here. Let’s take a look on the following type definition:
program Project26;
{$POINTERMATH ON}
type
TCalibrationTable = array [0 .. 3] of byte;
PCalibrationTable = ^TCalibrationTable;
Now I need 2 arrays, each of them having 3 elements of TCalibrationTable
(ultimately they are 2-dimensional, 3 × 4 arrays).
And also need a header which points to a pair of such array objects.
var
table0: array [0 .. 2] of TCalibrationTable;
table1: array [0 .. 2] of TCalibrationTable;
header: array [0 .. 1] of PCalibrationTable;
Let’s initialize the header: the most comfortable way to access an array by a pointer is to assign it the first element’s address, then simply index the pointer to get an arbitrary element.
begin
header[0] := @table0[0];
header[1] := @table1[0];
Now, header[0]
being a PCalibrationTable
, I can use it to index it in order to get arbitrary TCalibrationTable
typed elements from an array. In this way header[0][2]
SHOULD BE a TCalibrationTable
, that is, an array of 4 elements of byte
.
But the following assignment will give a build error: Array type required.
header[0][2][3] := 100;
end.
Why?
Upvotes: 0
Views: 237
Reputation: 8261
If you remove the pointer indirection from the header:
type TCalibrationTable = array [0 .. 3] of byte; PCalibrationTable = ^TCalibrationTable; var header: array [0 .. 1] of PCalibrationTable;
you get this:
type
TCalibrationTable = array [0 .. 3] of byte;
var
header: array [0 .. 1] of TCalibrationTable;
or
var
header: array [0 .. 1] of array [0 .. 3] of byte;
or
var
header: array [0 .. 1, 0 .. 3] of byte;
In other words: a 2-dimensional array.
What you need is this:
type
TCalibrationTable = array [0 .. 3] of byte;
TCalibrationArr = ARRAY[0..2] OF TCalibrationTable;
PCalibrationArr = ^TCalibrationArr;
var
table0: TCalibrationArr;
table1: TCalibrationArr;
header: array [0 .. 1] of PCalibrationArr;
you can then do:
header[0] := @table0;
header[1] := @table1;
header[0][1][2] := 123;
Upvotes: 1