Hi,
I have a question regarding arrays of characters passed as parameters to procedures.
If I declare:
Type Str40 = Array[1..40] Of Char; Str80 = Array[1..80] Of Char;
Var S40 : Str40; S80 : Str80; S : String(50);
Procedure DoArray(SParam : STRING); ..
Procedure DoArrayVar(VAR SParam : STRING); ...
I can call DoArray with S40, S80 and S as parameter with no problem, but I cannot call DoArrayVar with S40 or S80 (It gives the error: type mismatch in argument 1 of 'DoArrayVar' at compilation time). I don't understand why it works when the parameter is not VAR and why it does not work when it is VAR.
To have DoArrayVar work, I had to remove the parameter type, and assign the parameter S to a local variable pointer to a Str80. It works for S40 and S80 but not anymore for S.
Is there a way to declare a VAR parameter that would accept S40, S80 and S? Or is there a way to disable temporarily the type checking? Or something else ;-)
Thanks
Pascal Viandier pascal@accovia.com
Pascal Viandier wrote:
I have a question regarding arrays of characters passed as parameters to procedures.
If I declare:
Type Str40 = Array[1..40] Of Char; Str80 = Array[1..80] Of Char;
Var S40 : Str40; S80 : Str80; S : String(50);
Procedure DoArray(SParam : STRING); ..
Procedure DoArrayVar(VAR SParam : STRING); ...
I can call DoArray with S40, S80 and S as parameter with no problem, but I cannot call DoArrayVar with S40 or S80 (It gives the error: type mismatch in argument 1 of 'DoArrayVar' at compilation time). I don't understand why it works when the parameter is not VAR and why it does not work when it is VAR.
Types of `var' parameters have to match exactly `String' is not the same as `array of char'. Value parameters only need to be assignment compatible.
To have DoArrayVar work, I had to remove the parameter type, and assign the parameter S to a local variable pointer to a Str80. It works for S40 and S80 but not anymore for S.
Untyped parameters (if that's what you mean) are a low-level beast better left alone unless strictly needed.
Is there a way to declare a VAR parameter that would accept S40, S80 and S?
No, they're distinct types. To accept S40 and S80, you could use a conformant array, but S is still different.
I might help explaining what you actually want to achieve and why you think you need to break typing rules.
Frank
Hi,
In fact, I am converting existing SUN Pascal code to GNU Pascal. Among this, I have a bunch of utility routines which manipulate strings various ways. Some of them have built-in equivalent in gpc rts or gpc unit, but some others have no equivalent.
Apparently, SUN Pascal is not as strict on VAR parameter types as GNU Pascal. So, I have lots of issues compiling the existing code.
An example of my needs is a procedure similar to UpCaseString in gpc unit, which makes each first letter of each word in a string uppercase:
Procedure UpCaseEachWordFirstLetter(Var S : String200);
Where: Type String80 = Varying[200] Of Char;
This procedure accepts strings of any length (SUN Pascal varying strings) up to 200 characters.
Anyway, I tried the conformant array parameter type and it works perfectly well for my needs (even on type STRING(xx) if you mind).
I forgot conformant arrays usage since I had no need for them for 10 years (with Oregon Pascal).
Thanks
Pascal Viandier
-----Message d'origine----- De : gpc-owner@gnu.de [mailto:gpc-owner@gnu.de] De la part de Frank Heckenbach Envoyé : June 8, 2005 13:34 À : gpc@gnu.de Objet : Re: Question on procedure parameters
Pascal Viandier wrote:
I have a question regarding arrays of characters passed as parameters to procedures.
If I declare:
Type Str40 = Array[1..40] Of Char; Str80 = Array[1..80] Of Char;
Var S40 : Str40; S80 : Str80; S : String(50);
Procedure DoArray(SParam : STRING); ..
Procedure DoArrayVar(VAR SParam : STRING); ...
I can call DoArray with S40, S80 and S as parameter with no problem, but I cannot call DoArrayVar with S40 or S80 (It gives the error: type mismatch
in
argument 1 of 'DoArrayVar' at compilation time). I don't understand why it works when the parameter is not VAR and why it does not work when it is VAR.
Types of `var' parameters have to match exactly `String' is not the same as `array of char'. Value parameters only need to be assignment compatible.
To have DoArrayVar work, I had to remove the parameter type, and assign
the
parameter S to a local variable pointer to a Str80. It works for S40 and
S80
but not anymore for S.
Untyped parameters (if that's what you mean) are a low-level beast better left alone unless strictly needed.
Is there a way to declare a VAR parameter that would accept S40, S80 and
S?
No, they're distinct types. To accept S40 and S80, you could use a conformant array, but S is still different.
I might help explaining what you actually want to achieve and why you think you need to break typing rules.
Frank
Pascal Viandier wrote:
In fact, I am converting existing SUN Pascal code to GNU Pascal. Among this, I have a bunch of utility routines which manipulate strings various ways. Some of them have built-in equivalent in gpc rts or gpc unit, but some others have no equivalent.
Apparently, SUN Pascal is not as strict on VAR parameter types as GNU Pascal. So, I have lots of issues compiling the existing code.
An example of my needs is a procedure similar to UpCaseString in gpc unit, which makes each first letter of each word in a string uppercase:
Procedure UpCaseEachWordFirstLetter(Var S : String200);
Where: Type String80 = Varying[200] Of Char;
This procedure accepts strings of any length (SUN Pascal varying strings) up to 200 characters.
The (mostly) equivalent in GPC would be `String (200)'. I usually suggest to use this type since (in contrast to fixed arrays of chars, string types store the current length). String is a schematic type in EP (and thus GPC), so a parameter of type `String' (without given capacity) will accept all `String (n)' types, even for `var' parameters.
Anyway, I tried the conformant array parameter type and it works perfectly well for my needs (even on type STRING(xx) if you mind).
Fine. Yes, it also works for `String (n)', but such `var' parameters can't alter the length of the string.
Frank
Pascal Viandier wrote:
In fact, I am converting existing SUN Pascal code to GNU Pascal. Among this, I have a bunch of utility routines which manipulate strings various ways.
Maybe, user-defined operators are useful in doing this (section 6.3.2 of the gpc manual).
Regards,
Adriaan van Os
Frank Heckenbach wrote:
Pascal Viandier wrote:
Anyway, I tried the conformant array parameter type and it works perfectly well for my needs (even on type STRING(xx) if you mind).
Fine. Yes, it also works for `String (n)', but such `var' parameters can't alter the length of the string.
It looks that passing strings to conformant array parameters is a GPC extension. My impression is that this is forbidden by EP (and we accept such parameters even in EP mode).