Const array as a parameter

Merlin
Administrator
Avatar
Gender:
Age: 24
Posts: 1409
Registered: 03 / 2005
Subject:

Const array as a parameter

 · 
Posted: 13.10.2017 - 11:46  ·  #1
The following does not compile

Code
program Bug;

{$NOSHADOW}
{ $WG}                     {global Warnings off}

Device = mega128, VCC=5;
{ $BOOTRST $0F000}         {Reset Jump to $0F000}

Define_Fuses
//  Override_Fuses;
  NoteBook   = B;
  COMport    = USB;
  LockBits0  = [];
  FuseBits0  = [];
  FuseBits1  = [SPIEN, OCDEN];
  FuseBits2  = [];

Import SysTick, LCDport;

From System Import Float;

Define
  ProcClock      = 4608000;        {Hertz}
  SysTick        = 10;             {msec}
  StackSize      = $0064, iData;
  FrameSize      = $0064, iData;
  LCDport        = PortA;
  LCDtype        = 44780;
  LCDrows        = 2;              {rows}
  LCDcolumns     = 40;             {columns per line}

Implementation

{$IDATA}

{--------------------------------------------------------------}
{ Type Declarations }
type


{--------------------------------------------------------------}
{ Const Declarations }
const
  cTest : array[0..7] of byte =          ( %01010,
                                           %01010,
                                           %01010,
                                           %01010,
                                           %01010,
                                           %01010,
                                           %01010,
                                           %01010);
                                           
                                           
{--------------------------------------------------------------}
{ Var Declarations }
{$IDATA}
var

{--------------------------------------------------------------}
{ functions }

{--------------------------------------------------------------}
{ Main Program }
{$IDATA}
procedure RegisterSpecialChar( pChar: array[0..7] of byte );
begin
  LCDCharSet( #7, pChar[ 0 ],
                  pChar[ 1 ],
                  pChar[ 2 ],
                  pChar[ 3 ],
                  pChar[ 4 ],
                  pChar[ 5 ],
                  pChar[ 6 ],
                  pChar[ 7 ] );
end;

begin
  RegisterSpecialChar( cTest );
end Bug.


I get the error 'Array of type BYTE expected' in the second to bottom line. But cTest is an array of type byte. I get the same error for memory (structconst) variables too.
miparo
Administrator
Avatar
Gender:
Location: Germany
Age: 58
Posts: 959
Registered: 09 / 2007
Subject:

Re: Const array as a parameter

 · 
Posted: 13.10.2017 - 13:09  ·  #2
Hi Merlin,
ctest ist not the same type as your declaration pchar.
You must write:

Code

Type 
  Ttest : Array[0..7] of byte;

Const
  ctest : tTest = (........)

procedure RegisterSpecialChar( pChar: TTest);
....


RegisterSpecialChar(ctest);



miparo
Merlin
Administrator
Avatar
Gender:
Age: 24
Posts: 1409
Registered: 03 / 2005
Subject:

Re: Const array as a parameter

 · 
Posted: 13.10.2017 - 14:31  ·  #3
How are they different?

I am happy to follow your advice, but if I change things slightly

Code
procedure RegisterSpecialChar( var pChar: array[0..7] of byte ); 


this does compile. (Not sure if it actually works...)

If you are saying that I am not allowed to pass an array, the surely the error should be in the declaration of the function, not in the call of it?
rh
Administrator
Avatar
Gender:
Location: Germany
Age: 24
Homepage: e-lab.de
Posts: 5558
Registered: 03 / 2002
Subject:

Re: Const array as a parameter

 · 
Posted: 13.10.2017 - 16:36  ·  #4
Hello Merlin,

even though cTest and PChar are of the same construction, for the
compiler they aren't of the same type!
For arrays and records it makes sense to declare them as types
before you use them as Vars or Const, like Miparo states.
An exception maybe your (var pChar ...). This is a pointer in realty
where the restrictions of usage are somewhat relaxed.

"not passing an array" was not the content of Miparos statement.
Of course it can be used, but simply use predefined types....

rolf
Merlin
Administrator
Avatar
Gender:
Age: 24
Posts: 1409
Registered: 03 / 2005
Subject:

Re: Const array as a parameter

 · 
Posted: 13.10.2017 - 17:06  ·  #5
Hi Rolf.

Sorry, perhaps I was not clear. Obviously declaring a type that is a an array and passing that type is passing an array. Also I am very happy with the solution. After all you need to do that in Delphi too.

What I was really getting at is that if you declare

Code
procedure RegisterSpecialChar( pChar: array[0..7] of byte );


there seems to be no sensible of calling it, so I would prefer it if the error was flagged at the function declaration, not at the attempt to call it. The error produced let me down the wrong path.

Still, the issue is resolved and that is the important thing.
rh
Administrator
Avatar
Gender:
Location: Germany
Age: 24
Homepage: e-lab.de
Posts: 5558
Registered: 03 / 2002
Subject:

Re: Const array as a parameter

 · 
Posted: 15.10.2017 - 19:21  ·  #6
Hello Merlin,

yes, you are right. I makes no sense to allow such a construction
as a calling argument in a procedure. Maybe I will flag it in the future.

rolf
Selected quotes for multi-quoting:   0

Registered users in this topic

Currently no registered users in this section

The statistic shows who was online during the last 5 minutes. Updated every 90 seconds.
MySQL Queries: 15 · Cache Hits: 14   107   121 · Page-Gen-Time: 0.039643s · Memory Usage: 2 MB · GZIP: on · Viewport: SMXL-HiDPI