Introduction

If you want to learn from scratch, then first you should read my article Pointers and Unsafe Code in C#: everything you need know on  Gooroo Think because this article is continuation from there. But if you already have fair knowledge about pointer and writing unsafe code in C# then you are ready to go from here.

So here comes the fun part, in this article we are going to do more cool stuff with pointers. If you are defining and pointing your pointers, then for sure somewhere you will be manipulating them.

Now it’s time to learn how you can increment,  decrement,  add,  subtract and compare pointers.

Incrementing and Decrementing

 To perform increment or decrements on a pointer we use increment operator (++) and decrement operator (--). Void* pointers cannot be incremented are decremented because they do not have any underlying type but increment and decrement on pointers depends on size of underlying type.

 let me explain you that.

Question: Why address incremented by 4 of pointer to int?

Answer: Well, why the address incremented by 4 instead of 1 it’s because in C# int is a keyword that denotes underlying .Net type System.Int32 which of size32 bit. By means of 1byte = 8 bits  ,32 bits are equals to 4 bytes so size of int is 4 bytes.

As I told earlier the increment and decrement on pointers depends on sizeof underlying type, so the increment and decrement on pointer to int will be of 4 becuase size of int  is 4 bytes.  

Example : paste the following code inside of main method of your class

AخA
 
1
unsafe
2
       {
3
           int a = 10;     //value= 10;
4
           a++;            //value= 11;
5
           Console.WriteLine("\n\tBefore Increment");
6
           int* p = &a;               
7
           Console.WriteLine($"Value of p is: {*p} \t Address of p is:{(int)p}");
8
           
9
           ++p;
10
           Console.WriteLine("\n\tAfter Increment");
11
           Console.WriteLine($"Value of p is: {*p} \t Address of p is:{(int)p}\n");
12
           Console.WriteLine("\tCalculation");
13
           Console.WriteLine($"Size of int {sizeof(int)}");
14
           
15
           Console.WriteLine($"Actual address of 11 is: {(int)p - (int)(sizeof(int))} ");
16
       }

Result:






To increment or decrement the actual value at address you have to specify de-reference operator with increment operator to access the value.

Adding to Pointers

We can add value to any type of pointer except void* pointers. Value can be of type int, uint, long, ulong. When you add a value to pointer like p+=2 then address of pointer changed. The change in address depends on type of pointer just.

Suppose you have a pointer ‘p’ and you want to add a value 'n' into it, the change in address of pointer will result by adding n*sizeof(int).

Paste the following code in main method of your console app.

x
1
unsafe
2
        {
3
            int local = 40;    //local int var
4
            int* p = &local;   //pointer to int
5
            int n = 2;         //value to add in pointer
6
            int addressBefore = (int)p;                
7
            Console.WriteLine($"Address of p before adding 2:\t {addressBefore}");
8
9
            p += n;           //adding value to pointer//
10
            int addressAfter = (int)p;
11
            
12
            Console.WriteLine($"Address of p after adding 2:\t {addressAfter}\n");
13
            Console.WriteLine($"Difference b/w addresses:\t {addressAfter - addressBefore}");                
14
            Console.WriteLine($"2*sizeof(int):\t\t\t {2 * sizeof(int)}");
15
        }

Result:

Subtracting from Pointers

Subtracting from pointers same as adding but here we will be subtracting instead of adding. Any type of value from int, uint, long, ulong can be subtracted from pointers of any type except void* pointers. When you subtract a value to pointer like p -=2 then change in address of pointer will result by subtracting n*sizeof(int).

Example: Suppose we have to pointers and want to calculate the difference b/w values and addresses

14
 
1
unsafe
2
       {
3
           int local = 40;    //local int var
4
5
           int* p = &local;   //pointer to int
6
           int local2 = 50;    //local int var
7
           int* p2 = &local2;   //pointer to int
8
 
9
           long difference = p - p2;
10
           Console.WriteLine($"Address of p: {(int)p}");
11
           Console.WriteLine($"Address of p2: {(int)p2}");
12
           Console.WriteLine($"Difference b/w value: {*p2-*p}");
13
           Console.WriteLine($"Difference b/w addresses: {difference}");
14
        }

Pointer Comparison

Comparison b/w pointers is very simple and straightforward. As pointers contain addresses and by using de-reference (*) operator we can also get actual content at that address as well so we can compare both addresses and values by using following comparison operators.

                                                                                                                   == != < > <= >=

Example: Here we have to pointers p and p2 and different comparisons b/w addresses and values. Paste the following code in main method of your console app. 

13
 
1
unsafe
2
        {
3
            int local = 40;    //local int var
4
            int* p = &local;   //pointer to int
5
            int local2 = 50;    //local2 int var
6
            int* p2 = &local2;   //pointer to int
7
 
8
            Console.WriteLine($"p==p2: \t\t{p == p2}");
9
            Console.WriteLine($"p!=p2: \t\t{p != p2}");
10
            Console.WriteLine($"*p==*p2: \t{*p == *p2}");
11
            Console.WriteLine($"*p>*p2: \t{*p > *p2}");
12
            Console.WriteLine($"*p<*p2: \t{*p < *p2}");
13
        }

Result: