Solved

Explain the work with pointers in the following case

Posted on 1997-08-15
4
175 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to fill array with TArray.Create? 14 90
LAN or WAN ? 11 92
JAudiorecorder record freezing the app 29 71
IdTCPClient and IdTCPServer exchanging data on desktops and not Android 11 116
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
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 …

825 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