문제

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); 포인터 주소에서 현재 값을 제공합니다 ... 실제 주소를 표시하려면 어떻게해야합니까?

도움이 되었습니까?

해결책

Console.WriteLine(new IntPtr(cp));

다른 팁

관리 코드를 사용하면 쓰레기 수집가가 자유롭게 물건을 움직일 수 있습니다. 확인하십시오 주소가 중요한 상황에있는 경우 객체를 다운하십시오.

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

포인터를 바이트 유형으로 변환하십시오.

char c = 'c';

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

이것은 ... 0x123ABC와 같은 것을 표시합니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top