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

x
?
Solved

Pointers

Posted on 1998-08-06
4
Medium Priority
?
158 Views
Last Modified: 2010-04-04
Let say I have stored a set of data in a field of a record. Now I want to use these data in a procedure which will calculate the difference between two adjacent elements. How can I create such a procedure?
0
Comment
Question by:liau080698
[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
  • 3
4 Comments
 
LVL 8

Accepted Solution

by:
ZifNab earned 150 total points
ID: 1361632
? liau, can you be more specific, are you working with database or did you made a plain pascal record?

My guess is your working with a database.

If with database, just make a calculated field. In the OnCalc procedure of the table, just fill in the calculation you want to make of the two fields, like

procedure TForm1.Table1CalcFields(DataSet: TDataSet);
begin
 DataSet.NewCalcField := DataSet.Field1-DataSet.Field2;
end;


Or you can go to SQL and use SQL commands to get the difference.

Regards, Zif.
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1361633
mmm, reading your title again, this can't be a database question...

Just a pointer question?

Assume this :

type
  PMyData = ^TMyData;
  TMyData = record
   Value1 : integer;
   Value2 : integer;
  end;

var Pointer1, Pointer2 : PMyData

function difference(apointer1, apointer2: PMyData):interger;
begin
 result := apointer1^.value1 - apointer2^.value1;
end;


{fill records}

 Pointer1^.value1 := 30;
 Pointer1^.value2 := 40;

 Pointer2^.value1 := 10;
 Pointer2^.value2 := 20;


{function call}

 Mydifference := difference(pointer1,pointer2);

Euhm, let's assume that the syntax is correct (test it please, haven't done it myself, it's getting kinda late here), because it's a long time ago that I've used these things.

Zif.
0
 

Author Comment

by:liau080698
ID: 1361634
The data has been read from a text file and stored in a plain pascal record called data with 2 fields namely dat and next. What I actually did was the following:

TopPointer := Nil;
While not Eof(f1) do
Readln(f1, Element);
Push(TopPointer, Element)


Procedure Push(var TopPointer : ^Data; Element : real);
var
TempPointer := ^Data;

begin
  new(TempPointer);
  TempPointer^.dat := Element;
  TempPointer^.Next := TopPointer;
  TopPointer := TempPointer;
  Dispose(TempPointer);
end;


Now I require a procedure that could calculate the difference of all the adjacent data I have stored. If the data is stored in an array, I could create a loop within the procedure to do such a job since the array element has been numbered. How does one do a similar job if only the pointer of the record is passed into the procedure?
Thanks.

0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1361635
Hi liau,

Ok, I understand now :

Mostly we do it this way :

1. Store a the top of the stack into a variable :  eg. PStackTop
    PStackTop.Next := NIL;
2. POldRecord := PStackTop
2. If you add a record : eg NewRecord
    NewRecord.Next := NIL;
    OldRecord.Next := NewRecord;

Now to iterate through the list, just do

 PiterateRecord := PStackRecord
 while PiterateRecord.Next <> NIL then do begin
 ....
   PiterateRecord := PiterateRecord.next;
 end;

You see, the NIL is very important here, because it determines the end of the list....

What you also can do, but not so 'good' is to keep a pointer which addresses to the begin in the list, but also a pointer which addresses to the end of the list...

This way you iterate from beginpointer till you arrive on endpointer. But you should better use the method with nil.

PS. you can also add a .previous field... that way you can iterate both ways...

Regards, Zif.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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 tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
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