What are dynamic array operators in Pascal expressions?

Pascal dynamic arrays – like arrays in other programming languages – have several array manipulation methods. However, there is only one dynamic array operator that concatenates two arrays together: the + operator.

This operator is available in Delphi mode and must be enabled by the user.

It is also vital for array elements to be of similar data type, otherwise the program causes a compiler error.

Code

Let’s look at the code to see how dynamic arrays are concatenated in Pascal. We declare 3 arrays, and perform concatenation operations between them using the + operator.

program Hello;
// enable the dynamic array mode
{$mode objfpc}  
{$modeswitch arrayoperators}  
// declare arrays
var  
  a,b, arr : array of byte;  
 
begin  
  a:=[23, 45, 56];  
  b:=[13, 34, 45]; 
// use operator + 
  arr:=a + b;  
  writeln('Concatenated array =  ', arr);

Output:

Concatenated array =  [23, 45, 56, 13, 34, 45]

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved