
Basic Data Operation

This is a basic data operation. It represents a fundamental action on a basic data type.
You may see other such operations in the Basic Data Operations category, or:


Integer Operations
Arithmetic |
Comparison
Boolean Operations
Bitwise |
Logical
String Operations
Concatenation |
Interpolation |
Comparison |
Matching
Memory Operations
Pointers & references |
Addresses


In this task, the goal is to demonstrate common operations on pointers and references. These examples show pointer operations on the stack, which can be dangerous and is rarely done. Pointers and references are commonly used along with Memory allocation on the heap.

\Paraphrasing the C example:
\This creates a pointer to an integer variable:
int Var, Ptr, V;
Ptr:= @Var;
 
\Access the integer variable through the pointer: 
Var:= 3;
V:= Ptr(0);     \set V to the value of Var, i.e. 3
Ptr(0):= 42;    \set Var to 42
 
\Change the pointer to refer to another integer variable:
int OtherVar;
Ptr:= @OtherVar;
 
\Change the pointer to not point to anything:
Ptr:= 0;        \or any integer expression that evaluates to 0
 
\Set the pointer to the first item of an array: 
int Array(10);
Ptr:= Array;
\Or alternatively:
Ptr:= @Array(0);
 
\Move the pointer to another item in the array:
def IntSize = 4;        \number of bytes in an integer
Ptr:= Ptr + 3*IntSize;  \pointer now points to Array(3)
Ptr:= Ptr - 2*IntSize;  \pointer now points to Array(1)
 
\Access an item in the array using the pointer:
V:= Ptr(3);     \get third item after Array(1), i.e. Array(4)
V:= Ptr(-1);    \get item immediately preceding Array(1), i.e. Array(0)
]