Sign in Join

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

Mailing lists / fpc-pascal / 2009-03

Array initialization

9 messages · 7 people · –

Reply by email Subscribe to this list Your subscription

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

Francisco Reyes

On the following URL,
http://web.mit.edu/sunsoft_v5.1/www/pascal/lang_ref/ref_data.doc.html#1212, I found some different syntax to initialize an array.

Examples there..
var
c1 :  array[1..10] of char := '123456';
int2 : array[1..100] of integer := [50 of 1, 50 of 2];

Tried those, but didn't seem to to work.

Is a loop the only way to initialize all the values in an array other than
Values: array[1..2] of integer = (0,0);

In the program I am working on, Values will be an array of 128 integers and I would like to initialize them to 0.

Right now I just using:
for InitialLoop := 1 to 128 do
	Values[InitialLoop] :=0;

Reply by email

Fantomas
to Francisco Reyes

Hello, Francisco!

you wrote 9 lines
Is a loop the only way to initialize all the values in an array other than Values: array[1..2] of integer = (0,0); In the program I am working on, Values will be an array of 128 integers and I would like to initialize them to 0. Right now I just using: for InitialLoop := 1 to 128 do Values[InitialLoop] :=0;

As far as I know, global variables are initialised with zeros when an application starts. But to ensure your array is filled with zeros you can use FillChar:

FillChar(Values,SizeOf(Values),0)

Best regards, Fantomas

Reply by email

Andrew Brunner
to Francisco Reyes: http://web.mit.edu/sunsoft_v5.1/www/pascal/lang_ref/ref_data.doc.html#1212, I found some…
var
  c1 : array[1..10]  of char;
begin
  fillchar(c1[1],5,0);
  fillchar(c1[5],5,#32);

You might need @ sign in front of c1 though...
...or...

const
  c1:array[1..10] of char = '123456789a';
Francisco Reyes wrote 25 lines
On the following URL, http://web.mit.edu/sunsoft_v5.1/www/pascal/lang_ref/ref_data.doc.html#1212, I found some different syntax to initialize an array. Examples there.. var c1 :  array[1..10] of char := '123456'; int2 : array[1..100] of integer := [50 of 1, 50 of 2]; Tried those, but didn't seem to to work. Is a loop the only way to initialize all the values in an array other than Values: array[1..2] of integer = (0,0); In the program I am working on, Values will be an array of 128 integers and I would like to initialize them to 0. Right now I just using: for InitialLoop := 1 to 128 do        Values[InitialLoop] :=0; _______________________________________________ fpc-pascal maillist  -   http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply by email

leledumbo
to Andrew Brunner
On the following URL, http://web.mit.edu/sunsoft_v5.1/www/pascal/lang_ref/ref_data.doc.html#1212, I found some different syntax to initialize an array.

That's another Pascal extension from Sun. One of the problem (and power) of Pascal is that it's often extended arbitrarily, because no standard (other than too simple ISO 7185 and too complicated ISO 10260) exists.

Remember that FPC tries to be compatible with Delphi (at least until version
7) and Turbo Pascal (also version 7), while adding its own extensions.

c1 : array[1..10] of char := '123456';

In this case, FPC still follows (not 100 % conforms) the standard. c1 is a fixed length array, therefore only strings of the same length can be assigned to it. If you want to follow standards, pad it with spaces. Otherwise, use String[10].

int2 : array[1..100] of integer := [50 of 1, 50 of 2];

This is like C's array initialization (extended maybe), which is not supported. Either fill all or do it in a statement. You can fill some followed by FillXXX which is the most commonly used method as it doesn't involve looping (MUCH faster).

IMHO, FPC should consider this kind of extension though (partial filling).

Show quoted text 5 lines
begin fillchar(c1[1],5,0); fillchar(c1[5],5,#32); You might need @ sign in front of c1 though...

AFAIK, no. FillChar doesn't expect pointers, the above is already correct (note that it's syntactically correct, but it will fill the wrong data if you add @).

View this message in context: http://www.nabble.com/Array-initialization-tp22570315p22572320.html Sent from the Free Pascal - General mailing list .

Reply by email

Marc Santhoff
to Francisco Reyes: http://web.mit.edu/sunsoft_v5.1/www/pascal/lang_ref/ref_data.doc.html#1212, I found some…
Francisco Reyes wrote 10 lines
On the following URL, http://web.mit.edu/sunsoft_v5.1/www/pascal/lang_ref/ref_data.doc.html#1212, I found some different syntax to initialize an array. Examples there.. var c1 : array[1..10] of char := '123456'; int2 : array[1..100] of integer := [50 of 1, 50 of 2]; Tried those, but didn't seem to to work.
Use only '=' instread of ':='.

Working is e.g.:

<code>
vector: array[0..6] of longint = (1, 2, 3, 4, 5, 6, 7);
type
	particle_t = record
		name: array [0..Pred(16)] of char;
		longi: integer;
		pressure: float;
		temperature: double;
		lati: integer;
	end;

wbuf: array [0..Pred(NRECORDS_C)] of particle_t = ( (name:'zero'#0; longi:0; pressure:0.0; temperature:0.0; lati:0), (name:'one'#0; longi:10; pressure:1.0; temperature:10.0; lati:10), ...
</code>

Show quoted text 6 lines
Is a loop the only way to initialize all the values in an array other than Values: array[1..2] of integer = (0,0); In the program I am working on, Values will be an array of 128 integers and I would like to initialize them to 0.

Use fillchar() as others mentioned.

Marc Santhoff

Reply by email

Jürgen Hestermann
to Francisco Reyes
> FillChar(Values,SizeOf(Values),0) That worked. However, for an integer is fillword better?

A "fillword" procedure does not exist. FillChar is an anchient Pascal function to simply fill any arbitrary memory location of any size (determined by a variable) with all the same byte. It is very fast but there is no type checking at all (you can fill any type of variable) and I think there is no range checking either. So you should use SizeOf() to give the number of bytes to be written but in special circumstance you can also use a different value.

A similar procedure is move, which copies bytes from one memory location to another (again both determined by variables) also without type and range checking.

Ju"rgen Hestermann.

Reply by email

Marco van de Voort
to Jürgen Hestermann

In our previous episode, J�rgen Hestermann said:

Show quoted text 5 lines
>> FillChar(Values,SizeOf(Values),0) > That worked. > However, for an integer is fillword better? A "fillword" procedure does not exist.

http://www.freepascal.org/docs-html/rtl/system/fillbyte.html http://www.freepascal.org/docs-html/rtl/system/fillword.html http://www.freepascal.org/docs-html/rtl/system/filldword.html

(byte is the same as char)

A similar procedure is move, which copies bytes from one memory location to another (again both determined by variables) also without type and range checking.

Compare* and index* functions also belong in this category. In general you could say they are memory-block operations.

Reply by email