0

I'm getting an error I don't understand. I am trying to just assign a value to an element of an array in pascal.

function TestingThing() : Integer;
type
  IntegerArray  = array[0..$effffff] of Integer;
  PIntegerArray = ^IntegerArray;
var
  I: Integer;
  D: PIntegerArray;
begin
  for I := 0 to 5 do
    D[ I ] := I;
end; 

This gives me an error on the line

D[I] := I

Error: Incompatible types: got "LongInt" expected "IntegerArray"

4
  • 1
    D is pointer, so you need to dereference it D^[ I ] := I; Commented Dec 13, 2023 at 20:00
  • 1
    Why are you using pointer types in this case? Commented Dec 13, 2023 at 23:44
  • @DelphiCoder Because array[0..$effffff] is a bogus data type. @Anthony does not want to allocate space for and use 251658239 integers, but only six. Commented Dec 31, 2023 at 0:46
  • @KaiBurghardt your comment doesn't make any sense. How are you supposed to know that? Commented Dec 31, 2023 at 8:01

1 Answer 1

4

D is a pointer to an array of integers, it is not a pointer to a single integer, like you are expecting.

So, when you do D[I], you are indexing into D and the resulting element is an array, and you can't assign a value to an array, hence the error.

You need to either:

  • dereference D before you index into the array it points at:

    D^[ I ] := I;

  • or, you need to change D to be a PInteger that points at the 1st Integer in the array, and then D[I] will work as expected.

Sign up to request clarification or add additional context in comments.

1 Comment

And D must be initialized to point to some memory for the array.

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.