Recent

Author Topic: Local array only taking first four entries of static array  (Read 4245 times)

Harrison

  • New Member
  • *
  • Posts: 16
Local array only taking first four entries of static array
« on: July 29, 2010, 08:33:11 am »
Alright, in my program, I've got multiple static arrays ranging in size from 1 to 61. They are all array of string. Let's call them "Array1", "Array2", "Array3".

In a procedure, I have a local array that dependent on user input, is meant to become one of the arrays. So I have code like this:

Code: [Select]
if userinput = 1 then
  begin
    Areas := Array1;
  end
else if userinput = 2 then
  begin
    Areas := Array2;
  end
else if userinput = 3 then
  begin
    Areas := Array3;
  end;

for I := 0 to SizeOf(Areas) do
  begin
    AreaCombo.AddItem(Areas[I],AreaCombo);
  end;

However, only four entries get added to AreaCombo, and sending SizeOf to my debug box shows that it's only a size of 4.

Why is it limiting itself to only four?

Leledumbo

  • Hero Member
  • *****
  • Posts: 8799
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Local array only taking first four entries of static array
« Reply #1 on: July 29, 2010, 08:46:39 am »
If Areas is a dynamic array, SizeOf() will return size of pointer. This is due to dynamic arrays treated internally as pointers. Use Length() instead.

Harrison

  • New Member
  • *
  • Posts: 16
Re: Local array only taking first four entries of static array
« Reply #2 on: July 29, 2010, 08:56:17 am »
Ah, awesome, thanks.

Might I ask what exactly the use of pointers like that are?

Leledumbo

  • Hero Member
  • *****
  • Posts: 8799
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Local array only taking first four entries of static array
« Reply #3 on: July 29, 2010, 01:10:13 pm »
Quote
Might I ask what exactly the use of pointers like that are?
Pointers are (mainly) used for dynamic memory allocation. In modern Pascal, this includes dynamic array, ansi- and wide-strings and classes (add more if I miss the others).

 

TinyPortal © 2005-2018