Stan wrote: > I'd like to use routine R in any of several projects. But those > projects, while using arrays of type extended, define the arrays of > different lengths, e.g., one might define MyArray : array[1..500] of > extended while another might have AnotherArray : array[1..750] of > extended. > > I'd like to include my UnitR into the "uses" of any of my projects, > and not have a fixed array size in R's code that I'd have to change to > match whatever the calling routine is sending.
Then R should receive an open array of Extended. function R(const Data: array of Extended): Extended; var J: Cardinal; begin Assert(Length(Data) > 0); for J := Low(Data) to High(Data) do // Do stuff with Data[J] end; var a: array[0..1] of Extended; b: array[3..6] of Extended; c: array of Extended; begin R(a); R(b); R(c); end. > It seems I should pass the array to R using a pointer to the > calling routine's array and an integer parameter indicating the number > of elements in the array. My question(s): > > 1. Is this the correct approach? And if so... Well, it's _a_ correct approach. Not the ideal one for Delphi, though. > 2. What is the syntax for (1) the calling routine, and (2) routine R. > For example, if R (which is an object) has a method, ComputeValue, > should it be defined as > function ComputeValue ( P : pointer; N : integer ) : extended; No. You know what P will point to -- an Extended -- so let the compiler know as well. The more the compiler knows about your program, the more it can help you write it properly. function ComputeValue(const P: PExtended; const N: Cardinal): Extended; > and if so, how would I access element [J] of the calling program's > array? var Data: PExtended; J: Cardinal; begin Data := P; J := 0; while J <= N do begin // Do things with Data^; Inc(Data); Inc(J); end; end; type TExtendedArray = array[0..MaxInt div SizeOf(Extended) - 1] of Extended; PExtendedArray = ^TExtendedArray; var Data: PExtendedArray; J: Cardinal; begin Assert(N > 0); Data := PExtendedArray(P); for J := 0 to Pred(N) do // Do stuff with Data[J] end; > How would the calling program pass its array, say MyArray : > array[1..500] of extended, to R? Would it be: > MyResult := ComputeValue( @MyArray, 500 ); ?? That could work. I'd write it like this: Result := ComputeValue(@Data[1], Length(Data)); -- Rob _______________________________________________ Delphi mailing list -> Delphi@elists.org http://www.elists.org/mailman/listinfo/delphi