Solved

Explain the work with pointers in the following case

Posted on 1997-08-15
4
163 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
ID: 1341493
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
ID: 1341494
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
ID: 1341495
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
ID: 1341496
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

939 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

6 Experts available now in Live!

Get 1:1 Help Now