Share via


Value of a Pointer Variable (C# Programming Guide) 

Use the pointer indirection expression to obtain the variable at the ___location pointed to by a pointer. The expression takes the following form:

*unary expression

Parameters

  • *
    The unary indirection operator.
  • unary expression
    A pointer-type expression.

Remarks

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.

Example

In the following example, a variable of the type char is accessed using pointers of different types.

// 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);
    }
}

Sample Output

Note that the address of theChar will vary from run to run, because the physical address allocated to a variable can change.

Value of theChar = Z

Address of theChar = 12F718

Value of pChar = Z

Value of pInt = 90

See Also

Reference

Pointer Expressions (C# Programming Guide)
Pointer types (C# Programming Guide)
unsafe (C# Reference)
fixed Statement (C# Reference)
stackalloc (C# Reference)

Concepts

C# Programming Guide

Other Resources

Types (C# Reference)