다음을 통해 공유


포인터 변환(C# 프로그래밍 가이드)

다음 표에서는 미리 정의된 암시적 포인터 변환을 보여 줍니다. 메서드 호출, 할당문 등 많은 경우에 암시적 변환이 발생할 수 있습니다.

암시적 포인터 변환

From

To

모든 포인터 형식

void*

null

모든 포인터 형식

암시적 변환이 불가능한 경우에 변환을 수행하려면 캐스트 식을 통해 명시적 변환을 사용합니다. 다음 표에서는 이러한 변환을 보여 줍니다.

명시적 포인터 변환

From

To

모든 포인터 형식

임의의 다른 포인터 형식

sbyte, byte, short, ushort, int, uint, long 또는 ulong

모든 포인터 형식

모든 포인터 형식

sbyte, byte, short, ushort, int, uint, long 또는 ulong

예제

다음 예제에서는 int에 대한 포인터를 byte에 대한 포인터로 변환합니다. 포인터는 변수의 선두 주소 바이트를 가리키게 됩니다. 결과 포인터를 int의 크기인 4바이트까지 연속적으로 증가시키면 변수의 나머지 바이트를 표시할 수 있습니다.

// compile with: /unsafe
class ClassConvert
{
    static void Main() 
    {
        int number = 1024;

        unsafe 
        {
            // Convert to byte: 
            byte* p = (byte*)&number;

            System.Console.Write("The 4 bytes of the integer:");

            // Display the 4 bytes of the int variable: 
            for (int i = 0 ; i < sizeof(int) ; ++i)
            {
                System.Console.Write(" {0:X2}", *p);
                // Increment the pointer:
                p++;
            }
            System.Console.WriteLine();
            System.Console.WriteLine("The value of the integer: {0}", number);

            // Keep the console window open in debug mode.
            System.Console.WriteLine("Press any key to exit.");
            System.Console.ReadKey();
        }
    }
}
    /* Output:
        The 4 bytes of the integer: 00 04 00 00
        The value of the integer: 1024
    */

참고 항목

참조

포인터 식(C# 프로그래밍 가이드)

포인터 형식(C# 프로그래밍 가이드)

unsafe(C# 참조)

fixed 문(C# 참조)

stackalloc(C# 참조)

개념

C# 프로그래밍 가이드

기타 리소스

형식(C# 참조)