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