使用指针间接寻址表达式可获取位于指针所指向的位置的变量。该表达式采用如下形式:
参数
-
*
-
一元间接寻址运算符。
-
unary expression
-
指针类型表达式。
备注
示例
下面的示例使用不同类型的指针访问 char
类型的变量。
C# | 复制代码 |
---|
// compile with: /unsafe
|
C# | 复制代码 |
---|
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);
}
}
|
示例输出
注意,theChar
的地址在不同的运行中是不同的,因为分配给变量的物理地址可能会更改。
Value of theChar = Z
Address of theChar = 12F718
Value of pChar = Z
Value of pInt = 90
请参见