The @
operator in Pascal is called the address operator. We use this to access the memory address of a variable, function/procedure, or class method. We refer to the memory address of the variable, function/procedure, or class method as its pointer.
We can also use the
addr()
function to get the memory address of an object.
To assign a memory address of an object to a pointer, use the @
operator or addr()
function to reference the variable as shown below:
_ptr_ := @_obj_;
Or:
_ptr_ := addr(_obj_);
@_obj_
stores the memory location of _obj_
in _ptr_
.
Note:
- Using the
@
operator with a function or procedure returns its entry point. We generally use this to pass a function/procedure to an assembly language routine or an inline statement.- Using the
@
operator with a class method cannot return the address of an interface method. This is because its address is not known at the time of compilation.
Consider the code snippet below, which demonstrates the use of the @
operator:
program Example1;{$TYPEDADDRESS ON}varnum1: Integer = 10;ptrnum: ^Integer; //pointer to an integerbeginptrnum := @num1;writeln('The value deferenced by ptrnum is: ', ptrnum^);end.
A pointer ptrnum
is declared in line 5. ptrnum
is pointed to a variable num1
that is declared in line 4 using the @
operator. Dereferencing ptrnum
in line 8 prints the same value stored in num1
as the pointer pointed to num1
.