using System;
public class PtrIndexDemo {
unsafe public static void Main() {
int[] nums = new int[10];
// index pointer
Console.WriteLine("Index pointer like array.");
fixed (int* p = nums) {
for(int i=0; i < 10; i++)
p[i] = i; // index pointer like array
for(int i=0; i < 10; i++)
Console.WriteLine("p[{0}]: {1} ", i, p[i]);
}
// use pointer arithmetic
Console.WriteLine("\nUse pointer arithmetic.");
fixed (int* p = nums) {
for(int i=0; i < 10; i++)
*(p+i) = i; // use pointer arithmetic
for(int i=0; i < 10; i++)
Console.WriteLine("*(p+{0}): {1} ", i, *(p+i));
}
}
Just wonder how the code works?
As far as I know, the code below, p receives the address of the first element of the nums array. But how can p be indexed? Does it change the address of p?...What does it do and how it work? I am pretty sure that it doesn't work the same as array indexing. And what is this btw *(p+i)? Really some detail explanation.

New Topic/Question
Reply




MultiQuote







|