C#使用指针表达式

时间:2023-03-10 05:14:41
C#使用指针表达式

如何:获取指针变量的值

使用指针间接运算符可获取位于指针所指向的位置的变量。 表达式采用下面的形式,其中, p 是指针类型:

*p;

不能对除指针类型以外的任何类型的表达式使用一元间接寻址运算符。 此外,不能将它应用于 void 指针。

当向 null 指针应用间接寻址运算符时,结果将取决于具体的实现。

下面的示例使用不同类型的指针访问 char 类型的变量。

注意,theChar 的地址在不同的运行中是不同的,因为分配给变量的物理地址可能会更改。

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);
}
}
//out-------------
theChar 的值 = Z
theChar 的地址 = 12F718
pChar 的值 = Z
pInt 的值 =

如何:获取变量的地址

要获取计算结果为固定变量的一元表达式的地址,请使用 address-of 运算符 [&]:

int number;
int* p = &number; //取地址符: &

address-of 运算符仅适用于变量。 如果该变量是可移动变量,则在获取其地址之前,可以使用 fixed 语句暂时固定此变量。

确保初始化该变量是程序员的责任。 如果变量未初始化,编译器不会发出错误消息。

不能获取常数或值的地址。

此示例声明一个指向 int 的指针 p,并将整数变量 number 的地址赋值给该指针。 给 *p 赋值的结果是初始化变量 number。

如果对此赋值语句进行注释,则将取消对变量 number 的初始化,但是不会发出编译时错误。

注意该示例如何使用成员访问运算符 -> 来获取和显示存储在指针中的地址。

class AddressOfOperator
{
static void Main()
{
int number; unsafe
{
// Assign the address of number to a pointer:
int* p = &number; // Commenting the following statement will remove the
// initialization of number.
*p = 0xffff; // Print the value of *p:
System.Console.WriteLine("Value at the location pointed to by p: {0:X}", *p); // Print the address stored in p:
System.Console.WriteLine("The address stored in p: {0}", (int)p);
} // Print the value of the variable number:
System.Console.WriteLine("Value of the variable number: {0:X}", number); System.Console.ReadKey();
}
}
/* Output:
Value at the location pointed to by p: FFFF
The address stored in p: 2420904
Value of the variable number: FFFF
*/

如何:通过指针访问成员

要访问在不安全的上下文中声明的结构的成员,您可以使用以下示例中所示的成员访问运算符,其中,p 是指向包含成员 x 的结构的指针。

CoOrds* p = &home;
p -> x = 25; //member access operator ->

此示例声明并实例化了包含两个坐标(x 和 y)的结构CoOrds。 此示例通过使用成员访问运算符 -> 和一个指向实例 home 的指针为 x 和 y 赋值。

请注意,表达式 p->x 等效于表达式 (*p).x,使用这两个表达式可获得相同的结果。

struct CoOrds
{
public int x;
public int y;
} class AccessMembers
{
static void Main()
{
CoOrds home; unsafe
{
CoOrds* p = &home;
p->x = ;
p->y = ; System.Console.WriteLine("The coordinates are: x={0}, y={1}", p->x, p->y );
}
}
}

如何:通过指针访问数组元素

在不安全的上下文中,可通过使用指针元素访问来访问内存中的元素,如下面的示例所示:

       char* charPointer = stackalloc char[123];
for (int i = 65; i < 123; i++)
{
charPointer[i] = (char)i; //access array elements
}

方括号中的表达式必须能够隐式转换为 int、uint、long 或 ulong。 操作 p[e] 等效于 *(p+e)。 与 C 和 C++ 一样,指针元素访问不检查越界错误。

在此示例中,123 内存位置被分配给字符数组 charPointer。 该数组用于在两个 for 循环中显示小写字母和大写字母。

请注意,表达式 charPointer[i] 与表达式 *(charPointer + i) 等效,使用这两个表达式可获得相同的结果。

class Pointers
{
unsafe static void Main()
{
char* charPointer = stackalloc char[]; for (int i = ; i < ; i++)
{
charPointer[i] = (char)i;
} // Print uppercase letters:
System.Console.WriteLine("Uppercase letters:");
for (int i = ; i < ; i++)
{
System.Console.Write(charPointer[i]);
}
System.Console.WriteLine(); // Print lowercase letters:
System.Console.WriteLine("Lowercase letters:");
for (int i = ; i < ; i++)
{
System.Console.Write(charPointer[i]);
}
}
}
//Output----------------
大写字母:
ABCDEFGHIJKLMNOPQRSTUVWXYZ
小写字母:
abcdefghijklmnopqrstuvwxyz