Link to home
Start Free TrialLog in
Avatar of sassas081597
sassas081597

asked on

Explain the work with pointers in the following case

Hallo All

I have the following problem:

A function (for example GetBits(var P: Pointer)) returns me a pointer to a
number of bits (how many I know from the function GetSize : LongInt).
All I need is to read the Bits from P  twice: once as an array of Byte and
once as an array of LongInt. Who can explain me how can I get the 5th Byte
or the 3rd LongInt from the memory allocated for P.

Best wishes
Alexander
Avatar of icampbe1
icampbe1

Declare an array of each type that you want as a pointer type.

TYPE
   taByte = ARRAY [0..99] OF BYTE;
   taLInt = ARRAY [0..99] OF LONGINT;

   paByte = ^taByte;
   paLInt = ^taLInt;

VAR
  BArray: paByte;
  IArray: paLInt;

You pass BArray or IArray in the call (or whatever you want) and then use the array index:

  Something := BArray^[34];   {This assumes that BArray contains a valid pointer}

Hope this helps,

Ian C.

Avatar of sassas081597

ASKER

I do not know at design time the length of the array of bits. It can be 2 bytes as well as 100000 bytes. It depends on the result of the function getSize: LongInt. May be that it returns a number that exceeds the maximum length of an array.
ASKER CERTIFIED SOLUTION
Avatar of peter_vc
peter_vc

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I think you are right - this code is accepted by compiler. May be you can explain me why the code

lp:=lp+SizeOf(LongInt)

does not work in this case - the compiler stops. I think we write the same code.