We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

# Explain the work with pointers in the following case

on
Medium Priority
232 Views
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
Comment
Watch Question

## View Solution Only

Commented:
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.

Commented:
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.
Commented:
Here's how to do it for LongInt.  Just change your pointer type for BTYEs.

var
p : pointer;
lp : ^LongInt;
larray : array[1..10] of LongInt;
value : LongInt;
begin
larray[1] := 100;
larray[2] := 200;
larray[3] := 300;
larray[4] := 400;
p := @larray; { start of array memory - first element }

{ this is what your interested in }

lp := p;
Inc(lp); { point to second }
Inc(lp); { point to third }
Value := lp^;  { Value will be 300 }
end;

Not the solution you were looking for? Getting a personalized solution is easy.

Commented:
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.

##### Thanks for using Experts Exchange.

• View three pieces of content (articles, solutions, posts, and videos)
• Ask the experts questions (counted toward content limit)
• Customize your dashboard and profile