Export (0) Print
Expand All

How to: Obtain the Value of a Pointer Variable (C# Programming Guide)

Use the pointer indirection operator to obtain the variable at the location pointed to by a pointer. The expression takes the following form, where p is a pointer type:

*p;

You cannot use the unary indirection operator on an expression of any type other than the pointer type. Also, you cannot apply it to a void pointer.

When you apply the indirection operator to a null pointer, the result depends on the implementation.

In the following example, a variable of the type char is accessed by using pointers of different types. Note that the address of theChar will vary from run to run, because the physical address allocated to a variable can change.

// compile with: /unsafe
unsafe class TestClass
{
    static void Main()
    {
        char theChar = 'Z';
        char* pChar = &theChar;
        void* pVoid = pChar;
        int* pInt = (int*)pVoid;

        System.Console.WriteLine("Value of theChar = {0}", theChar);
        System.Console.WriteLine("Address of theChar = {0:X2}",(int)pChar);
        System.Console.WriteLine("Value of pChar = {0}", *pChar);
        System.Console.WriteLine("Value of pInt = {0}", *pInt);
    }
}
Value of theChar = Z 
Address of theChar = 12F718
Value of pChar = Z 
Value of pInt = 90 
Show:
© 2014 Microsoft