如何读取C#中指针的值并将其转换为字符串
本文关键字:转换 字符串 指针 何读取 读取 | 更新日期: 2023-09-27 18:26:07
如何定义指针并用它读/写其他变量?
如何在文本框中显示指针的值?
来自MSDN(注意,应该使用/unsafe
开关编译)
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);
通过调用&[变量名称]指针类型为[变量类型]*
long number = 3;
long* ptr = &number;
您可以更改指针的类型。这可能会以非常肮脏的结局结束。
int* dirtyPtr = ((int*)ptr) + 1; //Takes only the second half of the long(8 Bytes) as int(4 Bytes)
// +1 refers to the length to the type, so you go 4 Bytes ahead
您可以使用*[指针名称]=值更改指针后面的变量值
*dirtyPtr = 50;