How to access array elements using a pointer in C#?

In C#, accessing array elements using pointers requires understanding the key differences between arrays and pointers. An array name and a pointer to the same data type are not the same variable type. For example, int *p and int[] p represent different types. You can increment a pointer variable because it's not fixed in memory, but an array address is fixed in memory and cannot be incremented directly.

To access array elements using pointers in C#, you must use unsafe code and the fixed statement to pin the array in memory temporarily.

Syntax

Following is the syntax for accessing array elements using pointers −

unsafe {
   fixed (dataType* ptr = arrayName) {
      // Access elements using *(ptr + index)
      // or ptr[index]
   }
}

The fixed statement prevents the garbage collector from moving the array during pointer operations −

fixed (int* ptr = list) {
   int value = *(ptr + i);  // Dereference pointer
   int address = (int)(ptr + i);  // Get address
}

Using Pointers to Access Array Elements

Array Memory Layout with Pointer Access 5 list[0] 25 list[1] 15 list[2] *ptr *(ptr+1) *(ptr+2) ptr points to first element

Example

using System;

namespace UnsafeCodeApplication {
   class TestPointer {
      public unsafe static void Main() {
         int[] list = {5, 25, 15};
         fixed(int *ptr = list) {
            /* let us have array address in pointer */
            for ( int i = 0; i < 3; i++) {
               Console.WriteLine("Address of list[{0}]={1}",i,(int)(ptr + i));
               Console.WriteLine("Value of list[{0}]={1}", i, *(ptr + i));
            }
         }
      }
   }
}

The output of the above code is −

Address of list[0]=58736832
Value of list[0]=5
Address of list[1]=58736836
Value of list[1]=25
Address of list[2]=58736840
Value of list[2]=15

Alternative Pointer Syntax

You can also use array indexing notation with pointers −

Example

using System;

namespace UnsafeCodeApplication {
   class TestPointer {
      public unsafe static void Main() {
         double[] numbers = {1.5, 2.7, 3.9};
         fixed(double *ptr = numbers) {
            for (int i = 0; i < numbers.Length; i++) {
               Console.WriteLine("numbers[{0}] = {1}", i, ptr[i]);
               Console.WriteLine("Using pointer arithmetic: {0}", *(ptr + i));
            }
         }
      }
   }
}

The output of the above code is −

numbers[0] = 1.5
Using pointer arithmetic: 1.5
numbers[1] = 2.7
Using pointer arithmetic: 2.7
numbers[2] = 3.9
Using pointer arithmetic: 3.9

Key Rules for Pointer Array Access

  • Always compile with the /unsafe flag or enable unsafe code in project settings.

  • Use the fixed statement to pin array memory before accessing with pointers.

  • Pointer arithmetic: ptr + i moves the pointer by i * sizeof(dataType) bytes.

  • Both *(ptr + i) and ptr[i] syntax can access array elements.

Conclusion

Accessing array elements using pointers in C# requires unsafe code and the fixed statement to pin arrays in memory. While pointers offer direct memory access, they should be used carefully due to safety concerns and are typically reserved for performance-critical scenarios.

Updated on: 2026-03-17T07:04:35+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements