Pointer Notation
A pointer is a variable that holds the memory address of another type. In C#, pointers can only be declared to hold the memory addresses of value types (except in the case of arrays – see below).
Pointers are declared implicitly, using the ‘dereferencer’ symbol *, as in the following example:
int *p;
[Note that some coders place the dereferencer symbol immediately after the type name, eg.
int* p;
This variation appears to work just as well as the previous one.]
This declaration sets up a pointer ‘p’, which will point to the initial memory address of an integer (stored in four bytes).
The combined syntactical element *p (‘p’ prefixed by the dereferencer symbol ‘*’) is used to refer to the type located at the memory location held by p. Hence given its declaration, *p can appear in integer assignments like the following:
*p = 5;
This code gives the value 5 to the integer that was initialised by the declaration. It is important, however, not to confuse such an assignment with one in which the derefencer symbol is absent, e.g.
p = 5;
The effect of this assignment is to change the memory location held by p. It doesn’t change the value of the integer initialised by the original declaration; it just means that p no longer points to that integer. In fact, p will now point to the start of the four bytes present at memory location 5.
Another important symbol for using pointers is the operator &, which in this context returns the memory address of the variable it prefixes. To give an example of this symbol, the following code sets up p to point to integer i’s memory location:
int i = 5;
int *p;
p = &i;
Given the above, the code
*p = 10;
changes the value of i to 10, since ‘*p’ can be read as ‘the integer located at the memory value held by p’.
There is another important piece of notation for pointers. Pointers can be declared for structs , as in the following example (which uses the ‘Coords’ struct defined further below):
Coords x = new Coords();
Coords *y = &x;
One can then use the declared pointer y to access a public field of x (say z). This would be done using either the expression
(*y).z
or the equivalent expression, which uses the -> string:
y -> z
Incoming search terms:
- C# pointer notation (5)
- C# pointer operations (2)
- C# pointer noptation (2)
- c# pointer (2)
- pointer notation in c sharp (2)
- interview questions on Pointers in c# (1)
- how to set value of textbox in run time using cgi perl (1)
- csharp pointer to variable (1)
- pointer operations (1)
- c sharp pointer example (1)
- pointer operations c# (1)
- pointer opertions in c# (1)
- pointer symbol c# (1)
- pointer symbol in csharp (1)
- replace operations on pointer c# (1)
- C# ^% pointer notation (1)
- c# search memory value (1)
- c sharp pointer tutorial (1)
- c sharp pointers tutorial (1)
- c# "^" pointer notation (1)
- c# * pointer symbol (1)
- c# pointer operation (1)
- c# pointer tips (1)
- c# pointer to variable (1)
- c# pointer tutorials (1)
- C# pointers (1)
- C# pointers tutorials (1)
- tips pointer C# (1)
