Partager via


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.

Example

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 

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)