Recent

Author Topic: How to initialize the array  (Read 16102 times)

Doe

  • New Member
  • *
  • Posts: 17
How to initialize the array
« on: December 26, 2020, 07:16:09 pm »
Hello, I am a beginner and I was trying to get a value from the string and put it into the array. An error message "Warning: Variable "code" of a managed type does not seem to be initialized". How do I solve it?

program pra2;
uses SysUtils;
var
  code:TStringArray;
  j,i:integer;
  text:string;

begin
  text:='rafrgqaf';
  code[0]:=text[1];
  writeln(code[0]);
  readln();
  end.           

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: How to initialize the array
« Reply #1 on: December 26, 2020, 08:17:41 pm »
It's just a warning, in case you forgot to initialize the array (or other managed type, like String), and it can usually be either ignored or shut down by initializing it, for example with:
Code: Pascal  [Select][+][-]
  1. code := Default(TStringArray);

Note, though, that in your case you did really forget to initialize the array, and that in a way that might lead you to an error/exception. Instead, do this:

Code: Pascal  [Select][+][-]
  1. program pra2;
  2. uses SysUtils;
  3. var
  4.   code:TStringArray;
  5.   j,i:integer;
  6.   text:string;
  7.  
  8. begin
  9.   text:='rafrgqaf';
  10.   { Initialize the array to contain 1 element }
  11.   SetLength(code, 1);
  12.   { you'll still get the warning: just ignore it,
  13.     deactivate it or use Default() to pre-initialize }
  14.   code[0]:=text[1];
  15.   writeln(code[0]);
  16.   readln();
  17. end.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Doe

  • New Member
  • *
  • Posts: 17
Re: How to initialize the array
« Reply #2 on: December 26, 2020, 09:56:59 pm »
Got it, thanks a lot!

furious programming

  • Hero Member
  • *****
  • Posts: 858
Re: How to initialize the array
« Reply #3 on: December 28, 2020, 02:35:32 pm »
You can also use a special array constructor:

Code: Pascal  [Select][+][-]
  1. Code := TStringArray.Create('r', 'a', 'f');

for both literals and variables:

Code: Pascal  [Select][+][-]
  1. var
  2.   Code: TStringArray;
  3.   Foo: String = 'abc';
  4. begin
  5.   Code := TStringArray.Create(Foo[1], Foo[2], Foo[3]);
  6.  
  7.   Write(Code[0], Code[1], Code[2]); // prints "abc"
  8. end;
Lazarus 3.2 with FPC 3.2.2, Windows 10 — all 64-bit

Working solo on an acrade, action/adventure game in retro style (pixelart), programming the engine and shell from scratch, using Free Pascal and SDL. Release planned in 2026.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: How to initialize the array
« Reply #4 on: December 28, 2020, 02:40:57 pm »
You can also use a special array constructor [...]

Is that already available in 3.2.0? Man, I didn't know! I thought it was still Trunk-only :-[
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

furious programming

  • Hero Member
  • *****
  • Posts: 858
Re: How to initialize the array
« Reply #5 on: December 28, 2020, 02:50:24 pm »
It was available in the previous stable version of the FPC, so… yes — you can use it in 3.2.0. 8)
Lazarus 3.2 with FPC 3.2.2, Windows 10 — all 64-bit

Working solo on an acrade, action/adventure game in retro style (pixelart), programming the engine and shell from scratch, using Free Pascal and SDL. Release planned in 2026.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: How to initialize the array
« Reply #6 on: December 28, 2020, 03:44:12 pm »
It was available in the previous stable version of the FPC, so… yes — you can use it in 3.2.0. 8)

It seems I should re-read the docs more frequently:
Quote from: Free Pascal Reference Guide
As of version 3.0 of Free Pascal, Dynamic array types have a constructor. This is intrinsic, the compiler provides it. [...]
:-[ :-[ :-[
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Thaddy

  • Hero Member
  • *****
  • Posts: 14382
  • Sensorship about opinions does not belong here.
Re: How to initialize the array
« Reply #7 on: December 28, 2020, 04:16:30 pm »
Or the simplest, on declaration (from 3.2.0):
Code: Pascal  [Select][+][-]
  1. {$mode delphi}
  2. var
  3. a:array of integer = [1,2,3,4];
  4. i:integer;
  5. begin
  6.  for i in a do writeln(i);
  7. end.
And in objfpc mode:
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}
  2. var
  3. a:array of integer = (1,2,3,4);
  4. i:integer;
  5. begin
  6.  for i in a do writeln(i);
  7. end.
Note the different notation between the modes.

In your case this becomes something like this:
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}
  2. var
  3. a:array of AnsiChar = ('r', 'a', 'f');
  4. c:AnsiChar;
  5. begin
  6.  for c in a do writeln(c);
  7. end.
« Last Edit: December 28, 2020, 04:23:35 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

JanRoza

  • Hero Member
  • *****
  • Posts: 672
    • http://www.silentwings.nl
Re: How to initialize the array
« Reply #8 on: December 28, 2020, 04:20:39 pm »
Thaddy, you show Delphi mode twice and no objfpc mode.
(old clipboard content maybe?).  ;)
OS: Windows 10 (64 bit) / Linux Mint (64 bit)
       Lazarus 3.2 FPC 3.2.2
       CodeTyphon 8.40 FPC 3.3.1

Thaddy

  • Hero Member
  • *****
  • Posts: 14382
  • Sensorship about opinions does not belong here.
Re: How to initialize the array
« Reply #9 on: December 28, 2020, 04:23:49 pm »
corrected. tnx
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5486
  • Compiler Developer
Re: How to initialize the array
« Reply #10 on: December 29, 2020, 11:12:21 am »
Hello, I am a beginner and I was trying to get a value from the string and put it into the array. An error message "Warning: Variable "code" of a managed type does not seem to be initialized". How do I solve it?

program pra2;
uses SysUtils;
var
  code:TStringArray;
  j,i:integer;
  text:string;

begin
  text:='rafrgqaf';
  code[0]:=text[1];
  writeln(code[0]);
  readln();
  end.           

By default an array is empty, thus simply using code[0] would be an error. So you need to initialize its length using SetLength(code, xxx) where xxx is the length you want.

You can also use a special array constructor [...]

Is that already available in 3.2.0? Man, I didn't know! I thought it was still Trunk-only :-[

Since 3.2.0 I'd suggest to use the inline array constructor [ ... ] instead of TSomeArrayType.Create, because the former does not require a named type.

Thaddy

  • Hero Member
  • *****
  • Posts: 14382
  • Sensorship about opinions does not belong here.
Re: How to initialize the array
« Reply #11 on: December 29, 2020, 11:25:48 am »
Since 3.2.0 I'd suggest to use the inline array constructor [ ... ] instead of TSomeArrayType.Create, because the former does not require a named type.
Or, as I explained, depending on mode, with (..). Your suggestion covers Delphi syntax only.
But as explained above, I agree. It is all in my - corrected - example.
Nice work, btw, to implement this!
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5486
  • Compiler Developer
Re: How to initialize the array
« Reply #12 on: December 29, 2020, 11:30:14 am »
Since 3.2.0 I'd suggest to use the inline array constructor [ ... ] instead of TSomeArrayType.Create, because the former does not require a named type.
Or, as I explained, depending on mode, with (..). Your suggestion covers Delphi syntax only.
But as explained above, I agree. It is all in my - corrected - example.
Nice work, btw, to implement this!

I meant inline in code, not constant. For code it's always the [ ... ] syntax.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: How to initialize the array
« Reply #13 on: December 29, 2020, 12:17:42 pm »
My guess (and it's just that: a guess*) is that the OP's code is just an example and he will need to "transform" arbitrary strings with content unknown at compile time, so using a constructor won't be possible.

But it's good to know that it's already implemented, whether as a explicit Create or as an implicit simple assignment. And it's also good to know the implicit constructor was implemented for 3.2 ... I knew there was some thing about it which I wasn't sure whether it was already there or waiting in trunk ;)


*Fact is I can't imagine how (except in very special circumstances) it would be useful to move a string char by char to an array when one can easily access it that way by itself, but I suppose there must be some reason: otherwise the OP's question would make no sense :-X
« Last Edit: December 29, 2020, 12:20:57 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

simone

  • Hero Member
  • *****
  • Posts: 573
Re: How to initialize the array
« Reply #14 on: December 29, 2020, 12:22:27 pm »
Just for curiosity: why this syntax difference between Delphi and ObjFPC modes?
Microsoft Windows 10 64 bit - Lazarus 3.0 FPC 3.2.2 x86_64-win64-win32/win64

 

TinyPortal © 2005-2018