In Delphi/Free Pascal: is ^ an operator or does it simply denote a pointer type?
When ^
is used as part of a type (typically in a type or variable declaration) it means "pointer to".
Example:
type
PInteger = ^Integer;
When ^
is used as a unary postfix operator, it means "dereference that pointer". So in this case it means "Print what P
points to" or "Print the target of P
".
Example:
var
i: integer;
a: integer;
Pi: PInteger;
begin
i:= 100;
Pi:= @i; <<--- Fill pointer to i with the address of i
a:= Pi^; <<--- Complicated way of writing (a:= i)
<<--- Read: Let A be what the pointer_to_i points to
Pi^:= 200;<<--- Complicated way of writing (i:= 200)
writeln('i = '+IntToStr(i)+' and a = '+IntToStr(a));
Related videos on Youtube
Adam Scott Roan
hi there. i'm a hobbyist programmer. my tools-of-the-trade are typically as follows: 1) C 2) Pascal and its decedents (Delphi, Free Pascal, Lazarus.) I use C when I'm in Linux, and Delphi otherwise... but now with Lazarus becoming more and more mature, my Pascal skills have been intersecting with the *NIX world. my itinerary for "apps i have written" is slightly defunct after a major hard drive failure about ~1.5 years ago, however, here's a rough list of what I have completed... both past and future/present. 1) zsnes patch. i wrote a patch that sends ROM files to the client computer if it did not have the file locally. technically this is usually illegal but there's a method to detect copyrighted signatures in a ROM file, but I never implemented it. I am going to work on this again soon with added portability. 2) a simple binary clock. C+SDL. very simple and straightforward. 3) tic-tac-toe game with complete procedural AI routines. C+SDL+OpenGL. 4) misc. other softs that no one cares about! that's about it. i'm usually in the C++ channel helping others, so if you have a specific question, feel free to ask it there.
Updated on June 05, 2022Comments
-
Adam Scott Roan almost 2 years
In Delphi/Free Pascal: is ^ an operator or does it simply denote a pointer type?
Sample code
program Project1; {$APPTYPE CONSOLE} var P: ^Integer; begin New(P); P^ := 20; writeln(P^); // How do I read this statement aloud? P is a pointer? Dispose(P); readln; end
-
Warren P about 12 yearsIf you've used C or C++, you can translate ^ where you see it to basically the same thing as * except that ^ is postfix (written after) when used to dereference and before when used to declare a pointer to X, whereas C consistently you put the * before the thing it's dereferencing. X^ == (*X).
-
OnTheFly about 12 years
P^
reads [pointer]P
dereferenced. Confer:^T
reads reference to typeT
. First is for the expressions, but latter is for the declarations. -
Jørn E. Angeltveit about 12 yearsBTW. The power operator in Delphi is not
^
. You need to usefunction Power(x, y)
.
-
-
Adam Scott Roan about 12 yearsthanks, I was thinking of prefix vs postfix... I'm just confused with the notation for some reason!
-
Dampsquid about 12 yearsinterestingly though if the pointer is a pointer to a record either R^.value or R.value is allowed (delphi 7 anyway)
-
CodesInChaos about 12 years@Dampsquid Yes, the
.
operator implicitly dereferences the pointer to the left, if necessary. Unlike c where there is both.
and the dereferencing equivalent->
. | And when you get to function pointers, the syntax gets really confusing since even the address-taking operator@
can become optional. -
Adam Scott Roan about 12 yearsthanks again!!!* like I said, I was confused because I couldn't find a single piece of documentation stating that it was even an operator! but now it makes sense. :>
-
Adam Scott Roan about 12 years@Dampsquid that's quite weird... the notation must not carry levels of indirection, which is really weird.
-
OnTheFly about 12 yearsIMO, you are adding unnecessary complexities with prefix and postfix. This concept does not burden Pascal as language. @AdamScottRoan, re: membership operator
.
: Delphi does automatic dereferencing for pointers to structured types. -
CodesInChaos about 12 years@user539484 pascal has both prefix(
@
,+
,-
) and postfix(^
) operators. No idea what you mean. -
OnTheFly about 12 years@CodeInChaos, in Pascal,
^
is postfix-only. -
OnTheFly about 12 years@Johan, where did you see an operator there? Or expression? It is a declaration, duh!
-
Marco van de Voort over 7 yearsCodeInChaos: the "." pointer derefence is specific to Delphi dialects. In normal Pascals (including Delphi predecessor Turbo Pascal) you need to dereference and select field: recordptrvar^.fieldname. In freepascal it depends on dialect mode.
-
Marco van de Voort over 7 yearsJohan: as operator it is, type declaration (including casts) is a different story.