방법: 포인터 변수의 값 가져오기(C# 프로그래밍 가이드)
포인터가 가리키는 위치의 변수를 가져오려면 포인터 간접 참조 연산자를 사용합니다. 식의 형태는 다음과 같습니다. 여기에서 p는 포인터 형식입니다.
*p;
포인터 형식이 아닌 다른 형식의 식에는 단항 간접 참조 연산자를 사용할 수 없습니다. 또한 이를 void 포인터에 적용할 수 없습니다.
간접 참조 연산자를 null 포인터에 적용하면 구현에 따라 결과가 달라집니다.
예제
다음 예제에서는 서로 다른 형식의 포인터를 사용하여 char 형식의 변수에 액세스합니다. theChar의 주소는 실행할 때마다 달라집니다. 변수에 할당되는 실제 주소가 변경될 수 있기 때문입니다.
// compile with: /unsafe
unsafe class TestClass
{
static void Main()
{
char theChar = 'Z';
char* pChar = &theChar;
void* pVoid = pChar;
int* pInt = (int*)pVoid;
System.Console.WriteLine("Value of theChar = {0}", theChar);
System.Console.WriteLine("Address of theChar = {0:X2}",(int)pChar);
System.Console.WriteLine("Value of pChar = {0}", *pChar);
System.Console.WriteLine("Value of pInt = {0}", *pInt);
}
}