Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2008-12

Initializing Arrays?

3 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Joseph Montanez

I've already read that its not built into pascal but how do I get around this? If I have:

contacts : array [1 .. 256] of JSMContact;

the only way to initialize it is?

    with contacts[1] do
    begin
        Name := '';
        PhoneNo := '';
    end;

Also if I am passing a var to a procedure how would use a pointer as parameter?

{ Wont compile :( }

procedure JSMContactAdd(contact : ^JSMContact);
JSMContactAdd(@contacts[1]);
Jürgen Hestermann
to Joseph Montanez

I've already read that its not built into pascal but how do I get around this?

If you use AnsiStrings they should be always initialized (as far as I know). But most other (and all selfdefined) types are not.

If you want to initialize a whole array in one step you can use:

    fillchar(contacts,sizeof(contacts),#0);

which fills the whole array with zeros.
For the first element it could be

    fillchar(contacts[1],sizeof(contacts[1]),#0);

But if you have an array with lots of shortstrings it may be more efficient to do it as you wrote, because then only one byte per shortstring (the length byte) is written.

Also if I am passing a var to a procedure how would use a pointer as parameter?

To declare a var parameter you need to do this:

    procedure JSMContactAdd(var contact : JSMContact);

Defining a var parameter tells the compiler to use the pointer to the variable instead of coping it to a temporary (local) variable.

Jürgen Hestermann.

Andrew Brunner
to Joseph Montanez: I've already read that its not built into pascal but how do I get around this? If I have:…

I typically use Empty, SetSize, Copy, and IndexOf etc. Methods for all my DataTyped Arrays. The SetSize method sets the size and for each Item in the Array I call empty.

Empty(Var Item:TJSMContact); overload;
Empty(Var Items:TJSMContacts); overload;
var
  iLcv:integer;
begin
  For iLcv:=0 to High(Items) do
    Empty(Items[iLcv]);
  SetLength(Items,0);
end;
function SetSize(Var Items:TJSMContacts, Size:Integer):integer; overload;
var
  iLength:Integer;
  iLcv:integer;
begin
  iLength:=Length(Items);
  SetLength(Items,Size);
  for iLcv:=iLength to Size do
    Empty(Items[iLcv]);
end;
procedure Copy(Var Source,Destination:TJSMContact); overload;
begin
  Desintation.Name:=Source.Name;
  // etc
end;
Joseph Montanez wrote 26 lines
I've already read that its not built into pascal but how do I get around this? If I have: contacts : array [1 .. 256] of JSMContact; the only way to initialize it is? with contacts[1] do begin Name := ''; PhoneNo := ''; end; Also if I am passing a var to a procedure how would use a pointer as parameter? { Wont compile :( } procedure JSMContactAdd(contact : ^JSMContact); JSMContactAdd(@contacts[1]); _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal