Question:
How to declare a dynamic array in pascal compiler GDB?
2013-05-21 02:06:46 UTC
In my program I use

Var cars : array of string[42]

and free pascal compiler compiles it and runs it without any problem but when I send the program to a webpage with an automatic evaluating system that uses the GDB compiler, it cannot compile it and it says:

queue/47760.pas:5: error: syntax error before `of'
queue/47760.pas: In main program:
queue/47760.pas:23: error: undeclared identifier `cars' (first use in this routine)
queue/47760.pas:23: error: (Each undeclared identifier is reported only once
queue/47760.pas:23: error: for each routine it appears in.)
Three answers:
Ghroth
2013-05-21 04:08:42 UTC
For fixed size array, try:

var cars: array [0..42] of string;



or:

var cars: array of string;

{your function}

setlength(cars, 42);



For dynamic array you have to setlength(cars, i) through a loop:

for i := 0 to 42 do begin

setlength(cars, i + 1);

cars[i] := 'car ' + inttostr(i);

inc(i);

end;



Additinal Details:

Then it would be something like this:

var

i, n: integer;

cars: array of string;

begin

n := list.count; // just an example

for i := 0 to n - 1 do begin

setlength(cars, i + 1);

setlength(cars[i], 42); // same way to set length for a string like a dynamic array

cars[i] := 'mystring';

end;

end;
mcgilvery
2016-12-13 22:01:20 UTC
definite, yet its basically a play with words. in case you have been to create a String type. you are able to desire to define the two public and private get entry to operators. the interior maximum get entry to operators might do the authentic nitty gritty stuff with dynamic reminiscence allocation and practise, on an analogous time as the typical public get entry to operators might grant a extra basic interface to characteristic(), delete(), length() operators. So the programmer who gets to jot down the String type may well be no longer able to keep away from the pointer programming. regardless of the incontrovertible fact that, the programmer who gets to apply the String type might have a extra basic answer. The String type programmer provides you as many common public get entry to operations that they see in fantastic condition. they might have one called change_lenght() that grants you a public interface to the privately hidden dynamic reminiscence allocators/destructors written with practise and thoroughly written calls to malloc(), loose() and realloc().
Yi
2013-05-21 03:59:16 UTC
If I remember correctly, in pascal you cannot do this...


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...