如何在C#中显示指针地址? [英] How can I display a pointer address in C#?

查看:181
本文介绍了如何在C#中显示指针地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自从我用C#编程以来,我还没有做任何指针-我的C ++时代很久以前.我认为我应该刷新自己的知识,并且因为这里还有另一个问题而只是和他们一起玩.我对它们都了解,但是我不知道如何将指针的地址写入控制台...

I've not done any pointers since I've been programming in C# - and my C++ days were long ago. I thought I should refresh my knowledge and was just playing around with them because of another question on here. I understand them all okay, but I can't figure out how to write the pointer's address to the console...

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);给我指针地址的当前值...如果我想显示实际地址怎么办?

Using Console.WriteLine(*cp); gives me the current value at the pointer address... what if I want to display the actual address?

推荐答案

Console.WriteLine(new IntPtr(cp));

这篇关于如何在C#中显示指针地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆