Solved

Explain the work with pointers in the following case

Posted on 1997-08-15
4
156 Views
Last Modified: 2010-04-06
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
0
Comment
Question by:sassas081597
  • 2
4 Comments
 
LVL 2

Expert Comment

by:icampbe1
Comment Utility
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.

0
 
LVL 1

Author Comment

by:sassas081597
Comment Utility
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.
0
 
LVL 1

Accepted Solution

by:
peter_vc earned 100 total points
Comment Utility
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;

0
 
LVL 1

Author Comment

by:sassas081597
Comment Utility
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.

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now