none
Array Pointers

    Question

  • 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.

    fixed (int* p = nums)
    
    Thursday, May 05, 2011 9:56 PM

Answers

  • Imagine you have a few playing cards layed out in a row in front of you.  These represent the array. 
     
    Now put your index finger on a card in the middle of the line.  Your finger is "p" in your example.  It is a pointer, that is currently set to point at one of the cards.  The value of "p" is not the card you are pointing to, but rather your finger.  "p*" is the card your finger points to.  You could also use (p)* to indicate this same card.
     
    C/C++ allowed you to move one "card" or object left and right by adding and subtracting from your pointer.  The compiler needed to know what kind of objects you were moving through.  In our example you are moving through cards, so "p+1" means one card right of where you are pointing.  In this case it is an imaginary finger to the right of your index finger.  "(p+1)*" then in this example means the card that the imaginary index finger is pointing to.
     
    Hope this helps.

    --
    Mike
    • Marked as answer by Aspen VJ Monday, May 16, 2011 1:05 AM
    Friday, May 06, 2011 2:12 AM

All replies

  • The indexing operator comes from C.  In C, there wasn't a strong concept of an array.  An array was a block of memory whose size was determined by the type being stored and the number of them you wanted to store. 

    The array indexing operator p[i] is equivalent to *(p+i).  That is why array indexes are zero based.

    The * operator when it is used as a unary operator like *p is the derferencing operator.  If p is a 4 byte location in memory that is pointing to a 2 byte char at some address, then *p is the 2 byte character that p is pointing to.  p+i means the memory address that is n*i bytes away from p, where n is the memory size of the type of p (e.g., for an int, n would be 4).  So, *(p+i) is the value stored at the memory location n*i bytes away from p, where n is again the size of p.

    If you want to undersatand pointers, read up about them in C or C++.  Pointers in unsafe C# work substantially the same way.

    Evan

     


    Friday, May 06, 2011 12:29 AM
  • I still don't really understand the whole process...

    I think once the *p has been assigned value of the address of array, it will points to the first element of the array, but how can it be used to index the array elements? I just don't get it. Hope there will be a detail explanation (Be more general without jargon)

    Friday, May 06, 2011 1:55 AM
  • Imagine you have a few playing cards layed out in a row in front of you.  These represent the array. 
     
    Now put your index finger on a card in the middle of the line.  Your finger is "p" in your example.  It is a pointer, that is currently set to point at one of the cards.  The value of "p" is not the card you are pointing to, but rather your finger.  "p*" is the card your finger points to.  You could also use (p)* to indicate this same card.
     
    C/C++ allowed you to move one "card" or object left and right by adding and subtracting from your pointer.  The compiler needed to know what kind of objects you were moving through.  In our example you are moving through cards, so "p+1" means one card right of where you are pointing.  In this case it is an imaginary finger to the right of your index finger.  "(p+1)*" then in this example means the card that the imaginary index finger is pointing to.
     
    Hope this helps.

    --
    Mike
    • Marked as answer by Aspen VJ Monday, May 16, 2011 1:05 AM
    Friday, May 06, 2011 2:12 AM