RosettaCodeData/Task/Pointers-and-references/XPL0/pointers-and-references.xpl0
Ingy döt Net b83f433714 tasks a-s
2013-04-10 23:57:08 -07:00

32 lines
962 B
Text

\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)
]