Prof Abimbola Olowofoyeku wrote:
On 15 Mar 2002 at 17:48, Silvio a Beccara wrote:
Hello,
is there a way that dynamic arrays can be created (using schemata) which are GLOBAL, i.e. not accessible in the sole main program, but also in all functions and procedures included in it? For instance this little piece of code gives me this error:
You obviously come from a C background. First, your program is illegal (you cannot declare variables within a "begin .. end" block.
Well, actually you can in GPC, but that's a non-standard extension.
Secondly, the variable "vett" is used before being declared. If you want it to be visible throughout the program, you should declare it at the top of the program (before you start defining your procedures and functions).
Indeed. That's one problem -- because of the "declare before use" rule (which is the same in C BTW), variables declared in the statement part are never visible to subroutines.
The other problem is that GPC in fact does not support global variables of non-constant size. (This is planned, but since it will have to be implemented using a pointer internally, it's not completely trivial to do.)
The solution to both problems is the same: Declare a pointer variable (before the subroutine), and allocate it with `New' (e.g., `New (Vett, nelem)'). Of course, you then have to dereference it explicitly with `^' in every usage. Alternatively, you could pass it as a parameter to the procedure, like:
Procedure merce (var Vett: TVecDob);
[...]
merce (Vett^);
Then you only need `^' in the main program, not in the procedure. (Instead of a `var' parameter, you could also use `const' or `protected var', or even a value parameter if you want it to be copied. This works because the size is known when the procedure is called.)
Frank