Forum > Beginners

Resizing arrays.

(1/2) > >>

lmont761:
Hi everyone. Quick beginner question. If you have a dynamic array declared as a global named X, and you pass it by reference to a function, is it legal in Pascal to resize array X from inside the function?

Thanks.

howardpc:
If the array is declared as a global entity you don't need to pass a reference to it.
Your function can resize it by direct access.

Kays:

--- Quote from: lmont761 on January 13, 2022, 08:51:11 pm ---Hi everyone. Quick beginner question. If you have a dynamic array declared as a global named X, and you pass it by reference to a function, is it legal in Pascal to resize array X from inside the function?
--- End quote ---
Does the compiler emit an error? Then it’s legal. Please read the reference guide for further basic questions.

lmont761:
Thanks a lot guys!

Thaddy:

--- Quote from: jamie on January 14, 2022, 01:22:00 am ---if you mean something like this


--- 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";}};} ---procedure Setit(var X:Array of Byte);begin  SetLength(X,100);end;                        
--- End quote ---
That is wrong.
The parameter is not a dynamic array but an open array parameter.
See the manual: https://www.freepascal.org/docs-html/ref/refsu68.html
Use TByteArray as parameter and it works as you expect.

The latter part of the post seems that you already tried something in the good direction.
Here is an example how it should be done.

--- 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";}};} ---type TByteArray = array of byte;procedure Setit(var X:TByteArray);begin  SetLength(X,100);end;

Navigation

[0] Message Index

[#] Next page

Go to full version