C# Pointer Arithmetic — Complete Guide
In this tutorial, you'll learn about C# Pointer Arithmetic. We cover key concepts, practical examples, and best practices.
You have a pointer to the start of a buffer and need to navigate to specific offsets — reading a header, parsing a binary format, or scanning memory. You add offsets manually. Pointer arithmetic lets you navigate by adding or subtracting from the pointer directly.
Wrong
unsafe void ParseBuffer(byte* buffer, int length)
{
// Manual offset calculation
byte type = buffer[0];
int size = *(int*)(buffer + 1);
byte* data = buffer + 5;
}
Output: Works, but magic numbers everywhere. Hard to read and maintain.
Right
unsafe void ParseBuffer(byte* buffer, int length)
{
byte* ptr = buffer;
byte type = *ptr++; // Read and advance
int size = *(int*)ptr; // Read 4 bytes at current position
ptr += 4; // Advance 4 bytes
byte* data = ptr; // Remaining data
}
Output: Cleaner navigation. Each operation advances the pointer.
Pointer arithmetic respects the element size:
int* intPtr = (int*)buffer;
int first = *intPtr; // First int
int second = *(intPtr + 1); // Second int (4 bytes ahead)
intPtr += 10; // Advance 10 ints (40 bytes)
The difference between two pointers gives the element count:
long count = ptr2 - ptr1; // Number of elements between pointers
Prevention
- Use pointer arithmetic for navigating binary data structures.
- Use
++,--,+,-for pointer movement (respects element size). - Use
*ptrfor dereferencing (reading the value at the pointer). - Use
ptr[index]as an alternative to*(ptr + index). - Use
Span<T>andMemory<T>instead of raw pointers when possible. - Always check bounds before pointer arithmetic — no automatic bounds checking.
- Be careful with
void*— it has no element size, so arithmetic is not allowed.
Common Mistakes with pointer arithmetic
- Using
foldlinstead offoldl'causing stack overflow on large lists - Forgetting
deriving (Show, Eq)on custom data types needed for debugging - Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
These mistakes appear frequently in real-world CSHARP code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Pointer arithmetic is used in Durga Antivirus Pro's real-time scanner for fast memory pattern matching. For more C#, visit DodaTech.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro