Game Development Reference
at times. Because of this, it is important to briefly discuss pointer notation, though
only scratching the surface of a complex topic. If you are new to using pointers, it
is recommended that you do further reading before attempting to use them in
What is a pointer? A pointer is a variable that holds the memory address of another
type . In C#, pointers are implicitly declared using the dereferencer symbol (*).
After declaring the pointer variable, prefixing the variable with a dereferencer
symbol will allow you to refer to the type located at the memory location held by
the pointer; this is commonly known as dereferencing a pointer .
For example, the following code creates an integer with a pointer to it ( intPtr ) and
uses integer assignment to set its value to 27.
int* intPtr = 27;
Later on, should you wish to change the integer value, you can use the following
code to set the value to 15.
*intPtr = 15;
It is very important that you prefix the variable with the dereferencer symbol when trying to work
with the data.
Consider the following code:
intPtr = 56;
The intent was to set the integer value to 56, but in actuality the pointer will now point to the start
of the four bytes at memory location 56 (which could be anything).
Another symbol that is essential when working with pointers is the address operator
(&) (in the context of pointer notation). Prefixing a variable with this operator will
return the memory address of the variable.
The following code declares an integer and creates a pointer that points to the
location of the integer in memory.
int myNumber = 42;
myNumberPtr = &myNumber;
At this time, we have a pointer ( myNumberPtr ) that points at the memory location
of an integer ( myNumber ) in memory.