Recent

Author Topic: Pointer to a record  (Read 20936 times)

nebulom

  • New member
  • *
  • Posts: 5
Pointer to a record
« on: May 26, 2010, 03:00:55 pm »
I'm starting to learn object pascal and I have a little problems about pointers. I have this type:

Code: [Select]
TMailingListRecord = record
    FirstName: string;
    LastName: string;
    Address: string;
    City: string;
    State: string;
    Zip: integer;
  end;
  PMailingListRecord = ^TMailingListRecord; 
and when I access the property of the record after allocation it gives me an error:

Code: [Select]
ptr := allocmem(sizeof(TMailingListRecord));
  ptr.FirstName := 'Per';
  ptr.LastName := 'Larsen';
  showmessage(ptr.LastName + ', ' + ptr.FirstName);
  freemem(ptr);

The errors are:

Code: [Select]
Unit1.pas(85,7) Error: Illegal qualifier
Unit1.pas(85,7) Hint: may be pointer dereference is missing
Unit1.pas(85,7) Error: Illegal expression
Unit1.pas(85,7) Fatal: Syntax error, ";" expected but "identifier FIRSTNAME" found

ptr btw is ptr := PMailingListRecord. Can anybody point me to a right direction? Thanks.


Troodon

  • Sr. Member
  • ****
  • Posts: 484
Re: Pointer to a record
« Reply #1 on: May 26, 2010, 03:14:42 pm »
Indulge yourself like this:

Code: [Select]
type
  TMailingListRecord = record
    FirstName: string;
    // [...]
  end;
  PMailingListRecord = ^TMailingListRecord;
var
  ptr: PMailingListRecord;
begin
  ptr := New(PMailingListRecord);
  ptr^.FirstName := 'Per';
  // [...]
  Dispose(ptr);
end;
« Last Edit: May 26, 2010, 03:39:31 pm by Troodon »
Lazarus/FPC on Linux

nebulom

  • New member
  • *
  • Posts: 5
Re: Pointer to a record
« Reply #2 on: May 26, 2010, 04:21:17 pm »
Thanks. But isn't this valid object pascal code? It's valid in Delphi. I guess it's just implementation that differs. Anyway, thanks.

Troodon

  • Sr. Member
  • ****
  • Posts: 484
Re: Pointer to a record
« Reply #3 on: May 26, 2010, 04:33:42 pm »
I think you still need the "^". But that still looks like C-style programming to me. ;)

Thanks. But isn't this valid object pascal code? It's valid in Delphi. I guess it's just implementation that differs. Anyway, thanks.
Lazarus/FPC on Linux

eny

  • Hero Member
  • *****
  • Posts: 1628
Re: Pointer to a record
« Reply #4 on: May 26, 2010, 04:38:27 pm »
Why is it that everyone who wants to learn FPC/Lazarus (or almost any other language for that matter) has this morbid fascination of pointers  :(


All posts based on: Win10 (Win64); Lazarus 2.0.10 'stable' (x64) unless specified otherwise...

nebulom

  • New member
  • *
  • Posts: 5
Re: Pointer to a record
« Reply #5 on: May 26, 2010, 04:59:14 pm »
I just came across about a book of Delphi in 21 days and it discusses pointers. Though valid in delphi, there maybe some different implementation for freepascal. I'm not so sure if there is a committe body for standardization of pascal language as I'm just new to this language.

I'm not really an avid fan of pointer and I don't like it, it just so happens that it's in the tutorial and I don't want to miss out anything. Anyway, the code Troodon gave works so I'm moving on to classes.

And actually the implementation of class variables are pointers and the declaration of an instance of an object is a pointer referring to that of the class defined. So I guess, it's natural for me to come across with pointers. Anyway thanks for the help.

eny

  • Hero Member
  • *****
  • Posts: 1628
Re: Pointer to a record
« Reply #6 on: May 26, 2010, 05:11:14 pm »
I just came across about a book of Delphi in 21 days and it discusses pointers. Though valid in delphi, there maybe some different implementation for freepascal. I'm not so sure if there is a committe body for standardization of pascal language as I'm just new to this language.'
Ah, I should have said '... everyone that writes a book about Delphi/Pascal...'  :D

Quote
I'm not really an avid fan of pointer and I don't like it, it just so happens that it's in the tutorial and I don't want to miss out anything. Anyway, the code Troodon gave works so I'm moving on to classes.
Now the fun really starts  :)

Quote
And actually the implementation of class variables are pointers and the declaration of an instance of an object is a pointer referring to that of the class defined. So I guess, it's natural for me to come across with pointers. Anyway thanks for the help.
It's mainly a matter of definition. Personally I prefer the term 'object reference'.
Nevertheless: knowledge about pointers can be useful.
« Last Edit: May 26, 2010, 05:15:59 pm by eny »
All posts based on: Win10 (Win64); Lazarus 2.0.10 'stable' (x64) unless specified otherwise...

Troodon

  • Sr. Member
  • ****
  • Posts: 484
Re: Pointer to a record
« Reply #7 on: May 26, 2010, 08:29:18 pm »
Maybe because pointers, along with recursion, are the most powerful features in computer programming? :)

You need pointers to create graphs (the most complex data structures). And with a few lines of recursive code you can traverse a graph.

Why is it that everyone who wants to learn FPC/Lazarus (or almost any other language for that matter) has this morbid fascination of pointers  :(
Lazarus/FPC on Linux

eny

  • Hero Member
  • *****
  • Posts: 1628
Re: Pointer to a record
« Reply #8 on: May 27, 2010, 02:57:03 am »
Maybe because pointers, along with recursion, are the most powerful features in computer programming? :)
I've used Delphi/Lazarus for many years and there was almost never any reason to use 'pure' pointers (the one exception being the datastructure that one needs with VirtualTrees).
If by pointers you also mean object references/variables, I agree.
All posts based on: Win10 (Win64); Lazarus 2.0.10 'stable' (x64) unless specified otherwise...

Leledumbo

  • Hero Member
  • *****
  • Posts: 8428
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Pointer to a record
« Reply #9 on: May 27, 2010, 05:42:24 am »
Quote
Thanks. But isn't this valid object pascal code? It's valid in Delphi. I guess it's just implementation that differs. Anyway, thanks.
Use {$mode delphi} or {$mode tp} for this. FPC and OBJFPC strives to be stricter than Delphi/TP, I think because it may cause confusion when you need both the pointer and the pointed value.

 

TinyPortal © 2005-2018