若要获取计算结果为固定变量的一元表达式的地址,请使用 address-of 表达式。该表达式采用的形式为:
参数
-
&
-
address-of 运算符
-
unary expression
-
计算结果为 fixed 变量的表达式。
备注
示例
此示例声明一个指向 int 的指针 p
,并将整数变量 number
的地址赋值给该指针。变量 number
被初始化为对 *p 赋值的结果。注释掉此赋值语句将取消对变量 number
的初始化,但是不会发出编译时错误。注意该示例如何使用成员访问运算符 -> 来获取和显示存储在指针中的地址。
C# | 复制代码 |
---|
// compile with: /unsafe
|
C# | 复制代码 |
---|
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}", p->ToString());
}
// Print the value of the variable number:
System.Console.WriteLine("Value of the variable number: {0:X}", number);
}
}
|
输出
Value at the location pointed to by p: FFFF
The address stored in p: 65535
Value of the variable number: FFFF
请参见