自从我开始使用C#编程以来,我就没有使用过指针 - 而我的C++时代已经很久远了。我想我应该刷新一下我的知识,并且只是因为这里的另一个问题而玩弄它们。我都能理解它们,但我无法弄清楚如何将指针地址写入控制台...
char c = 'c';
char d = 'd';
char e = 'e';
unsafe
{
char* cp = &d;
//How do I write the pointer address to the console?
*cp = 'f';
cp = &e;
//How do I write the pointer address to the console?
*cp = 'g';
cp = &c;
//How do I write the pointer address to the console?
*cp = 'h';
}
Console.WriteLine("c:{0}", c); //should display "c:h";
Console.WriteLine("d:{0}", d); //should display "d:f";
Console.WriteLine("e:{0}", e); //should display "e:g";
使用 Console.WriteLine(*cp);
可以输出指针地址上的当前值,但如果我想显示实际地址怎么办?