Jossi
Jossi

Reputation: 1080

Position attribute for record component offset

How to get offset in bytes for a record component?

From Ada Programming/Attributes/'Position

'Position is a record type component attribute. It represents the address offset of the component from the beginning of the record. The value returned is represented in storage units, which is machine-specific.

Compiling this code:

with Ada.Integer_Text_IO;
use Ada.Integer_Text_IO;
procedure Main is
   type R is record
      I : Integer;
      F : Float;
   end record;
begin
   Put(R.I'Position); --9
   Put(R.F'Position); --10
end;

results in:

main.adb:9:08: invalid prefix in selected component "R"
main.adb:10:08: invalid prefix in selected component "R"

I don't know why I cant compile that?

For example, equivalently look at C++ offsetof documentation.

Upvotes: 1

Views: 814

Answers (1)

egilhh
egilhh

Reputation: 6430

If you look in the Reference Manual (RM 13.5.2), you'll see that R.C'Position is defined

For a component C of a composite, non-array object R

which means it won't work for a type as in your code. You need to create a variable:

   Foo : R;
begin
   Put(Foo.I'Position); --9
   Put(Foo.F'Position); --10

So the example in the wikibook seems to be wrong.

Upvotes: 2

Related Questions