C#에서 배열 이름과 배열 데이터와 동일한 데이터 형식에 대한 포인터는 동일한 변수 형식이 아닙니다. 예를 들어, int *p와 int[] p는 같은 유형이 아닙니다. 포인터 변수 p는 메모리에 고정되어 있지 않지만 배열 주소는 메모리에 고정되어 있기 때문에 증가할 수 있으며 증가할 수 없습니다.
다음은 예입니다 -
예
using System;
namespace UnsafeCodeApplication {
class TestPointer {
public unsafe static void Main() {
int[] list = {5, 25};
fixed(int *ptr = list)
/* let us have array address in pointer */
for ( int i = 0; i < 2; i++) {
Console.WriteLine("Address of list[{0}]={1}",i,(int)(ptr + i));
Console.WriteLine("Value of list[{0}]={1}", i, *(ptr + i));
}
Console.ReadKey();
}
}
} 출력
다음은 출력입니다 -
Address of list[0] = 31627168 Value of list[0] = 5 Address of list[1] = 31627172 Value of list[1] = 25