Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Explain the work with pointers in the following case

Posted on 1997-08-15
4
Medium Priority
?
217 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 300 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

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…
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…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

618 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