On 15 Aug 2005 at 2:07, Frank Heckenbach wrote:
Prof. Harley Flanders wrote:
{ This program compiles and runs with Free Pascal. See GPC compile error below.
Apparently GPC doesn't line "array of string" although it doesn't choke on the declaration }
Which version? Mine does:
gpc-20050331
foo.p:10: error: syntax error before `of'
var A: array of string;
Array bounds are missing.
I get a syntax error, but also an undeclared identifier.
If this is yet another form of dynamic arrays,
It is. It was introduced in Delphi 4 (1998). See this example:
program dynarr; Procedure foo (Var a: Array of String); Var i : Cardinal; begin for i := low (a) to high (a) do writeln (i, '. ', a [i]); end;
VAR A : Array [1..10] of String; { zero-based } A1 : Array of String; begin foo (A); { 0... 9 } SetLength (A1, 5); foo (A1); { 0 .. 4 } end.
This is from the Delphi4 help file:
"In Delphi 4, in addition to declaring static arrays such as A: array[1..100] of string;
you can now declare dynamic arrays. Dynamic arrays specify type information (the number of dimensions and the type of the elements) but not the number of elements. Thus: A: array of integer; B: array of array of string;
declares two dynamic arrays. A is a one-dimensional arrays of integers, while B is a two dimensional array of strings. For more information, see Dynamic arrays."
"Dynamic Arrays: Dynamic arrays do not have a fixed size or length. Instead, memory for a dynamic array is reallocated when you assign a value to the array or pass it to the SetLength procedure. Dynamic-array types are denoted by constructions of the form
array of baseType
For example, var MyFlexibleArray: array of Real;
declares a one-dimensional dynamic array of reals. The declaration does not allocate memory for MyFlexibleArray. To create the array in memory, call SetLength. For example, given the declaration above,
SetLength(MyFlexibleArray, 20);
allocates an array of 20 reals, indexed 0 to 19. Dynamic arrays are always integer-indexed, always starting from 0. Dynamic-array variables are implicitly pointers and are managed by the same reference-counting technique used for long strings. To deallocate a dynamic array, assign nil to a variable that references the array or pass the variable to Finalize; either of these methods disposes of the array, provided there are no other references to it. Dynamic arrays of length 0 have the value nil. Do not apply the dereference operator (^) to a dynamic-array variable or pass it to the New or Dispose procedure.
If X and Y are variables of the same dynamic-array type, X :=Y allocates X to the length of Y and points X to the same array as Y. Unlike strings and static arrays, dynamic arrays are not automatically copied before they are written to. For example, after this code executes
var
A, B: array of Integer; begin SetLength(A, 1); A[0] := 1; B := A; B[0] := 2; end;
the value of A[0] is 2. (If A and B were static arrays, A[0] would still be 1.) Assigning to a dynamic-array index (for example, MyFlexibleArray[2] := 7) does not reallocate the array. Out-of-range indexes are not reported at compile time. When dynamic-array variables are compared, their references are compared, not their array values. Thus, after execution of the code
var
A, B: array of Integer; begin SetLength(A, 1); SetLength(B, 1); A[0] := 2; B[0] := 2; end;
A = B returns False but A[0] = B[0] returns True. To truncate a dynamic array, pass it to the Copy function and assign the result back to the array variable. For example, if A is a dynamic array, A := Copy(A, 0, 20) truncates all but the first 20 elements of A. Once a dynamic array has been allocated, you can pass it to the standard functions Length , High, and Low. Length returns the number of elements in the array, High returns the arrays highest index (that is, Length 1), and Low returns 0. In the case of a zero-length array, High returns 1 (with the anomalous consequence that High < Low).
Note: In some function and procedure declarations, array parameters are represented as array of baseType, without any index types specified. For example,
function CheckStrings(A: array of string): Boolean;
This indicates that the function operates on all arrays of the specified base type, regardless of their size, how they are indexed, or whether they are allocated statically or dynamically. "
Best regards, The Chief -------- Prof. Abimbola A. Olowofoyeku (The African Chief) web: http://www.greatchief.plus.com/