Recent

Author Topic: [SOLVED]Const Array of Const Records, all fields have to be filled individually?  (Read 2786 times)

BrainChemistry

  • Jr. Member
  • **
  • Posts: 79
Hi there,

I'm aware this works, and there are thousands of examples like this on the internet:
Code: Pascal  [Select][+][-]
  1. type
  2.   TPoint = record
  3.      X, Y: Single;
  4.   end;
  5.  
  6. const
  7.   Points: array[0..2] of TPoint = ((X: 0.0; Y: 0.0),  (X: 1.0; Y: 1.0),  (X: 2.0; Y: 2.0))
  8.  

Though what I can't get running is something like this:
Code: Pascal  [Select][+][-]
  1. type
  2.   TPoint = record
  3.      X, Y: Single;
  4.   end;
  5.  
  6. const
  7.   Point1: TPoint = (X: 0.0; Y: 0.0);
  8.   Point2: TPoint = (X: 1.0; Y: 1.0);
  9.   Point3: TPoint = (X: 2.0; Y: 2.0);
  10.  
  11.   Points: array[0..2] of TPoint = (Point1, Point2, Point3);
  12.  

Also trying to have Points not being a const but a var. didn't work unless I specify all the fields individually (as seen in first code example):
Code: Pascal  [Select][+][-]
  1. type
  2.   TPoint = record
  3.      X, Y: Single;
  4.   end;
  5.  
  6. const
  7.   Point1: TPoint = (X: 0.0; Y: 0.0);
  8.   Point2: TPoint = (X: 1.0; Y: 1.0);
  9.   Point3: TPoint = (X: 2.0; Y: 2.0);
  10.  
  11. var
  12.   Points: array[0..2] of TPoint = (Point1, Point2, Point3);
  13.  


Since I couldn't find any example on the internet, I guess it's not possible, or what am I missing here?

Thanks in advance.
« Last Edit: August 12, 2018, 05:53:00 pm by BrainChemistry »

440bx

  • Hero Member
  • *****
  • Posts: 3944
Hi,

The compiler is doing the right thing and the reason what you want and, are trying to do doesn't work is a bit subtle but not much.

The declaration:
Code: Pascal  [Select][+][-]
  1. Points: array[0..2] of TPoint = (Point1, Point2, Point3);
is invalid because the only way to make it valid is to _copy_ Point1, Point2 and Point3 into the Points array.  The compiler won't do that for you.

What you're trying to do can be done if you change the declarations a bit.  The following example works and does what you are trying to do (albeit in a different way than the one you attempted):
Code: Pascal  [Select][+][-]
  1. program InitializedStucturedConstants;
  2.  
  3. type
  4.   PPoint = ^TPoint;
  5.   TPoint = record
  6.     X, Y: Single;
  7.   end;
  8.  
  9. const
  10.   Point1: TPoint = (X: 0.0; Y: 0.0);
  11.   Point2: TPoint = (X: 1.0; Y: 1.0);
  12.   Point3: TPoint = (X: 2.0; Y: 2.0);
  13.  
  14. //Points: array[0..2] of TPoint = (Point1, Point2, Point3);
  15.  
  16. Points   : array[0..2] of PPoint = (@Point1, @Point2, @Point3);
  17.  
  18. begin
  19.   with Points[1]^ do writeln('X : ', X, '  Y : ', Y);
  20. end.
  21.  

To reference the points you've declared, you need to use pointers to them. You cannot use the points themselves because then the const Point1 and the array element Point1 are two _distinct_ items in memory, not the same one which is probably what you want it to be.

HTH.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

BrainChemistry

  • Jr. Member
  • **
  • Posts: 79
That's brilliant, thanks.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Since I couldn't find any example on the internet, I guess it's not possible, or what am I missing here?

440bx already provided you with a solution, nevertheless as an explanation: your Point1, etc. declarations are so called typed constants (they have a : typename). However to use them inside another constant declaration you need to use untyped constants (without : typename, though this isn't supported for structured types).

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Code: Pascal  [Select][+][-]
  1. Or
  2.  
  3.     type
  4.       TPoint = record
  5.          X, Y: Single;
  6.       end;
  7.      
  8.     const
  9.       Points: array[0..2] of TPoint = ((X: 0.0; Y: 0.0),  (X: 1.0; Y: 1.0),  (X: 2.0; Y: 2.0));
  10.  
  11.        var
  12.         Point1: TPoint absolute points[0];
  13.         Point2: TPoint absolute points[1];
  14.         Point3: TPoint absolute points[2];
  15.  
  16. begin
  17.   writeln(point2.x,' ',point3.x);
  18. end.

Though this might not work on targets where typed constants are not in the datasegment (?).

440bx

  • Hero Member
  • *****
  • Posts: 3944
Though this might not work on targets where typed constants are not in the datasegment (?).
I'm only familiar with how the compiler works under Windows.  Given that these "constants" really aren't constants, just initialized variables, are there targets where they are not in a writeable data segment ?
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Though this might not work on targets where typed constants are not in the datasegment (?).
I'm only familiar with how the compiler works under Windows.  Given that these "constants" really aren't constants, just initialized variables, are there targets where they are not in a writeable data segment ?

microcontrollers often have a lot more flash than memory.

 

TinyPortal © 2005-2018