logo
CSharp_Prog_Guide

Результат

The 4 bytes of the integer: 00 04 00 00

The value of the integer: 1024

Pointer Expressions

How to: Obtain the Value of a Pointer Variable

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