Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

pascal

What is the @ operator in Pascal?

Syed Muhammad Ali Mustafa

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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.

Assigning a memory address to a pointer

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.

Code

Consider the code snippet below, which demonstrates the use of the @ operator:

program Example1;
{$TYPEDADDRESS ON}
var
num1: Integer = 10;
ptrnum: ^Integer; //pointer to an integer
begin
ptrnum := @num1;
writeln('The value deferenced by ptrnum is: ', ptrnum^);
end.

Explanation

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.

RELATED TAGS

pascal

CONTRIBUTOR

Syed Muhammad Ali Mustafa
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring