Forum > General
Array of Integer as parameter accepts Integers
Fibonacci:
Seems like an Integer can be passed to a procedure that accepts an Array of Integers. Why is that?
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---uses SysUtils; procedure test(a: array of integer);var i: integer;begin writeln('len = ', length(a), ', size = ', sizeof(a):3); for i := 0 to high(a) do writeln(i, ' = ', inttohex(a[i]));end; begin //test(1); // project1.lpr(14,9) Error: Incompatible type for arg no. 1: Got "ShortInt", expected "{Open} Array Of LongInt" test(integer(1)); //test($ffff); // project1.lpr(16,13) Error: Incompatible type for arg no. 1: Got "Word", expected "{Open} Array Of LongInt" test(integer($ffff)); test($ffff+1); test(123456789); test([1, $ffff, $ffff+1, 123456789]); readln;end.
--- Quote ---len = 1, size = 4
0 = 00000001
len = 1, size = 4
0 = 0000FFFF
len = 1, size = 4
0 = 00010000
len = 1, size = 4
0 = 075BCD15
len = 4, size = 16
0 = 00000001
1 = 0000FFFF
2 = 00010000
3 = 075BCD15
--- End quote ---
MarkMLl:
Seems fairly logical to me: you've declared the parameter to accept any number of integers in an array. This might be mode-specific: what compiler mode are you using (and what compiler version etc.)?
MarkMLl
ASerge:
--- Quote from: Fibonacci on September 27, 2024, 12:31:10 pm ---Seems like an Integer can be passed to a procedure that accepts an Array of Integers. Why is that?
--- End quote ---
Yes, this is not reflected in the documentation.
For example from Delphi 7 documentation:
--- Quote ---Within the body of a routine, open array parameters are governed by the following rules.
* They are always zero-based. The first element is 0, the second element is 1, and so forth. The standard Low and High functions return 0 and Length - 1, respectively. The SizeOf function returns the size of the actual array passed to the routine.
* They can be accessed by element only. Assignments to an entire open array parameter are not allowed.
* They can be passed to other procedures and functions only as open array parameters or untyped var parameters. They cannot be passed to SetLength.
* Instead of an array, you can pass a variable of the open array parameter's base type. It will be treated as an array of length 1.
--- End quote ---
I have bold the last point. FPC extended this action not only for variables, but also for constants, but only those for which the type is set.
Fibonacci:
Kudos ASerge, always like your responses
PascalDragon:
--- Quote from: Fibonacci on September 27, 2024, 12:31:10 pm ---Seems like an Integer can be passed to a procedure that accepts an Array of Integers. Why is that?
--- End quote ---
Open array parameters are essentially a pointer to the first element and an additional argument with the high value of the array. It's logical that one can also pass single elements as essentially a single element array. This is compatible to Delphi and works in any mode that supports open array parameters. If it's not documented then someone please file a bug report against the documentation.
Navigation
[0] Message Index
[#] Next page