Jump to content
tdisque

Newbie question about data types/structures

Recommended Posts

I have used C for years but I'm only now learning to use Delphi.  I would appreciate any tips on potential pitfalls.

 

I have seen examples using pointer dereferences, such as "*p" in C vs. "p^" in Delphi, but what is the Delphi equivalent of "p = p->next;"?

 

I have tried converting some C code to Delphi, but I keep getting a "E2029 Statement expected but type found at line" message.  As far as I can tell, type is a legal statement,

Here is the code fragment:


 

procedure TElements.Button1Click(Sender: TObject);
begin

  // No element has more than 10 oxidation states
  Const MAX_OXIDATION_STATES:integer = 10;
  Const TOTAL_ELEMENTS:integer = 118;

  var AtomicNumber1:integer := 0;
  var AtomicNumber2:integer := 0;
  var leastCommonMultiple:integer;

  var ratio1:integer;
    var ratio2:integer;
    var Combinations:integer;
    var valence1:integer;
    var valence2:integer;
    var v1:integer;
    var v2:integer;

  var errorMessage:string;

  // Instantiate 118 objects, one for each element
  type AllElements = record
       Number:    integer;  // Atomic number
     Symbol:    string;   // Element symbol
     Name:      string;   // Element name
       AlphaSeq:  integer;  // Name alphabetical sequence number
     Mass:      double;   // Atomic number
     Oxidation: string;   // Oxidation states
     end;

Any suggestions?  Thanks for any tips!

 

Tom

Edited by Sherlock
Please consider using the coe tags for better readability

Share this post


Link to post
1 hour ago, tdisque said:

what is the Delphi equivalent of "p = p->next;"?

In C/C++, the '->' operator is primarily just a combination of the '*' dereference and '.' member-access operators (but can be overloaded in C++ for other purposes), eg the above can be written as follows instead and the functionality would be exactly the same:

p = (*p).next;

In Delphi, those same operations are expressed individually, there is no combination operator, eg:

p := p^.next;

However, in this case, the '^' is optional when accessing a member via a pointer, eg:

p := p.next;
Quote

I have tried converting some C code to Delphi, but I keep getting a "E2029 Statement expected but type found at line" message.  As far as I can tell, type is a legal statement,

There is no such thing as inline type declarations in Delphi, so you must use the traditional 'type' section to define the local record type, eg:

procedure TElements.Button1Click(Sender: TObject);
type
  AllElements = record
    Number:    integer;  // Atomic number
    Symbol:    string;   // Element symbol
    Name:      string;   // Element name
    AlphaSeq:  integer;  // Name alphabetical sequence number
    Mass:      double;   // Atomic number
    Oxidation: string;   // Oxidation states
  end;
begin
  ...
end;

Or, move the declaration outside of the procedure, eg:

type
  AllElements = record
    Number:    integer;  // Atomic number
    Symbol:    string;   // Element symbol
    Name:      string;   // Element name
    AlphaSeq:  integer;  // Name alphabetical sequence number
    Mass:      double;   // Atomic number
    Oxidation: string;   // Oxidation states
  end;

procedure TElements.Button1Click(Sender: TObject);
begin
  ...
end;

That being said, I also notice is that you are using inline const/variable declarations when you don't actually need to.  You are not using inline declarations the way they were intended to be used, so you may as well just use the more traditional 'const' and 'var' sections instead, eg:

procedure TElements.Button1Click(Sender: TObject);
type
  ...
const
  // No element has more than 10 oxidation states
  MAX_OXIDATION_STATES: integer = 10;
  TOTAL_ELEMENTS: integer = 118;
var
  AtomicNumber1: integer;
  AtomicNumber2: integer;
  leastCommonMultiple: integer;
  ratio1: integer;
  ratio2: integer;
  Combinations: integer;
  valence1: integer;
  valence2: integer;
  v1: integer;
  v2: integer;
  errorMessage: string;
begin
  AtomicNumber1 := 0;
  AtomicNumber2 := 0;
  ...
end;

 

Edited by Remy Lebeau
  • Like 1

Share this post


Link to post

Thank you very much!  If I can repay your kindness with any C or assembly language questions, I would be happy to do so.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×