943,704 Members | Top Members by Rank

Ad:
Sep 27th, 2009
-1

Delphi re-indexes arrays passed to functions?

Expand Post »
Hi all,

I don't use Delphi often so this is probably a stupid question again, but why does Delphi (7) re-index arrays when they're passed to a function, as a simplified example...

Pascal and Delphi Syntax (Toggle Plain Text)
  1. program example;
  2. {$APPTYPE CONSOLE}
  3. uses
  4. SysUtils;
  5. var
  6. numbers: array[1..5] of Integer = (10, 20, 30, 40, 50);
  7.  
  8. procedure example(var nums: array of Integer);
  9. begin
  10. WriteLn(nums[5]); //Undefined. 50 -> nums[4]
  11. end;
  12.  
  13. begin
  14. Writeln(numbers[5]);
  15. example(numbers);
  16. Readln;
  17. end.

Why does it do this? Thanks in advance for any answers.
Last edited by TomRandall; Sep 27th, 2009 at 2:59 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
TomRandall is offline Offline
9 posts
since Nov 2008
Sep 28th, 2009
0

Re: Delphi re-indexes arrays passed to functions?

Hello
this happend because an array index in Pascal by default start from 0
your array in Main program Start From 1 But the array that is defined in procedure is started from 0 By Default
so if you want to print fifth element of array in procedure you mast print num[4] in procedure.
Reputation Points: 12
Solved Threads: 10
Junior Poster in Training
fayyaz is offline Offline
66 posts
since Jun 2007
Sep 28th, 2009
0

Re: Delphi re-indexes arrays passed to functions?

What if the added parameter type is not meet properly?
Listen,if you create a new type as array and pass it to the proc...
delphi Syntax (Toggle Plain Text)
  1.  
  2. program array1___;
  3. {$APPTYPE CONSOLE}
  4. uses
  5. SysUtils;
  6.  
  7. type TArray = array[1..5]of integer;
  8.  
  9. const numbers:TArray = (10,20,30,40,50);
  10.  
  11.  
  12. procedure example(const nums: TArray);
  13. begin
  14. WriteLn(nums[5]);
  15. end;
  16.  
  17. begin
  18. Writeln(numbers[5]);{result is 50}
  19. example(numbers);{here too}
  20. Readln;
  21. end.
Reputation Points: 132
Solved Threads: 138
Posting Pro
FlamingClaw is offline Offline
559 posts
since Feb 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Pascal and Delphi Forum Timeline: TCP server with md5 Authentication + 3des crypt
Next Thread in Pascal and Delphi Forum Timeline: How can I list the sorted results on the screen?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC