How can I display a pointer address in 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';

    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";

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));

Remember that with managed code the garbage collector is free to move things around on you. Make sure to pin your object down if your in a situation where the address matters.

char* cptr;
char achr = 'a';
cptr = &achr;
string strcptr = Convert.ToString((long)cptr, 16);
Console.WriteLine("Ox{0} is the char ptr hex address", strcptr);

Convert your pointer to byte type.

char c = 'c';

  Console.WriteLine("0x{0:x}", (ulong)&c);

This will display something like ... 0x123abc (twice)

Need Your Help

How do you prevent a windows from being moved?

c# .net winforms user-interface

How would i go about stopping a form from being moved. I have the form border style set as FixedSingle and would like to keep it this way because it looks good in vista :)

Android - new BitmapDrawable deprecated; alternative Bitmap.createBitmap has to have w/h > 0

android bitmap warnings deprecated bitmapdrawable

I've used a PopupWindow. With this PopupWindow I set the BackgroundDrawable to an empty BitmapDrawable.