Working with the * and & Operators
Once you have established an unsafe context, you are then free to build pointers to data types using
the * operator and obtain the address of said pointer using the & operator. Unlike in C or C++, using
C#, the * operator is applied to the underlying type only, not as a prefix to each pointer variable
name. For example, consider the following code, which illustrates the correct and incorrect way to
declare pointers to integer variables:
// No! This is incorrect under C#!
int *pi, *pj;
// Yes! This is the way of C#.
int* pi, pj;
Consider the following unsafe method:
unsafe static void PrintValueAndAddress()
{
int myInt;
// Define an int pointer, and
// assign it the address of myInt.
int* ptrToMyInt = &myInt;
// Assign value of myInt using pointer indirection.
*ptrToMyInt = 123;
Do'stlaringiz bilan baham: |