[fpc-pascal] Problem with array of const

pascalX at piments.com pascalX at piments.com
Sun Oct 29 04:33:11 CET 2017


On 29/10/17 00:31, Vojtěch Čihák wrote:
> Looking to docs: https://www.freepascal.org/docs-html/ref/refsu69.html
> 
> I tried writeln(AnsiString(args[0].VAnsiString));
> 
> or just writeln(String(args[0].VAnsiString));
> 
> which works.
> 
> V.
> 
> ______________________________________________________________
>  > Od: Darius Blaszyk <dhkblaszyk at zeelandnet.nl>
>  > Komu: FPC-Pascal users discussions <fpc-pascal at lists.freepascal.org>
>  > Datum: 29.10.2017 00:59
>  > Předmět: [fpc-pascal] Problem with array of const
>  >
> 
> Consider the application below. When I run it I do get the following output:
> 
> name
> rg��������name�F&{00000000-0000-0000-C000-000000000046}
> 
> In other words I lose the first character (a) from the arguments 
> supplied and the string returns with a lot of garbage. What am I doing 
> wrong here?
> 
> Rgds, Darius
> 
> program test_args;
> 
> procedure test(name: string; args: array of const);
> begin
>    writeln(name);
>    writeln(args[0].VString^);
> end;
> 
> begin
>    test('name', ['arg']);
> end.
> 
> 
> ----------
> 
> _______________________________________________
> fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
> http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
> 
> 
> _______________________________________________
> fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
> http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
> 


       vtString     : (VString: PShortString);


in short the compiler is  sending  AnsiString and you are then telling 
it to interpret this as a traditional pascal string of length 97.  The 
Tvarrec  in the Array of const effectively removes the usual strong type 
checking you get with Pascal.

string literals now get compiled as AnsiString unless you explicitly 
assign them to a typed constant:


program test_args;
{$mode objfpc}

procedure test(name: string; args: array of const);
begin
   writeln(name);
   writeln(args[0].VString^);
end;

const  sh:shortstring='sconst';
var s:shortstring='arg';

begin
   test('name', [s]);
   test('name', [sh]);
end.





More information about the fpc-pascal mailing list