Delphi re-indexes arrays passed to functions?

Please support our Pascal and Delphi advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Nov 2008
Posts: 9
Reputation: TomRandall is an unknown quantity at this point 
Solved Threads: 0
TomRandall TomRandall is offline Offline
Newbie Poster

Delphi re-indexes arrays passed to functions?

 
-1
  #1
Sep 27th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 36
Reputation: fayyaz is an unknown quantity at this point 
Solved Threads: 6
fayyaz fayyaz is offline Offline
Light Poster

Re: Delphi re-indexes arrays passed to functions?

 
0
  #2
Sep 28th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 460
Reputation: FlamingClaw will become famous soon enough FlamingClaw will become famous soon enough 
Solved Threads: 111
FlamingClaw's Avatar
FlamingClaw FlamingClaw is offline Offline
Posting Pro in Training

Re: Delphi re-indexes arrays passed to functions?

 
0
  #3
Sep 28th, 2009
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...
  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.
Be a good part of the community.Don't be ungrateful.
If you ask something on the forum and you got the right answer then mark as solved!
If my opinion helped to you a lot then sometimes give reputation point to me.
I'm just a pascal programmer from Hungary.
Farewell...
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Pascal and Delphi
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC