how to: increment and decrement pointers
Use the increment and decrement operators + + and --to change the position of the pointer of the pointer-type* type to sizeof (pointer-type). the increment and decrement expressions are in the form of the following:
++p;p++;--p;p--;
The increment and decrement operators can be applied to pointers of any type except the void* type.
The effect of applying the increment operator to a pointer of type Pointer-type is to increase the address contained in the pointer variable to sizeof (pointer-type).
The effect of applying a decrement operator to a pointer of type Pointer-type is to subtract sizeof (Pointer-type) from the address contained in the pointer variable.
When an operation overflows the pointer range, no exception is generated, and the actual result depends on the implementation.
Example
This example iterates through an array by adding the pointer to the size of an int. for each step, this example displays the address and contents of the array element.
classincrdecr{unsafe Static voidMain () {int[] numbers = {0,1,2,3,4}; //Assign The array address to the pointer: fixed(int* P1 =numbers) { //Step through the array elements: for(int* P2=P1; P2<p1+numbers. Length; p2++) {System.Console.WriteLine ("value:{0} @ address:{1}", *P2, (Long) p2); } } }}//Output--------------------Value:0@ Address:12860272Value:1@ Address:12860276Value:2@ Address:12860280Value:3@ Address:12860284Value:4@ Address:12860288
arithmetic operations of pointers
This topic discusses using arithmetic operators + and - to manipulate pointers. you cannot perform any arithmetic operation on a void pointer.
perform an add-minus value operation on a pointer
n of type int, uint, long, or ulong to A pointer, p,of any type except void*. " > a value of type int, uint, long, or ulong can be n and void* any type of pointer p and added. p+n is the pointer resulting from adding n * sizeof (p) to the address of P. " > p+n is a pointer to a n * sizeof (p) to the address of P.
Similarly,p-n is a pointer minus n * sizeof (p) from the address of P.
Subtraction of pointersOperation
You can also subtract from a pointer of the same type. the type of the computed result is always long. For example, if both P1 and P2 are pointers of type pointer-type*, the expression P1-P2 evaluates to:
(long) P1-(long) P2)/sizeof (pointer_type)
When an arithmetic operation overflows the pointer range, no exception is generated, and the result depends on the implementation.
classpointerarithmetic{unsafe Static voidMain () {int* Memory =stackalloc int[ -]; Longdifference; int* P1 = &memory[4]; int* P2 = &memory[Ten]; Difference= P2-P1; System.Console.WriteLine ("The difference is: {0}", difference); }}//output:the Difference is:6
Pointer comparison
The following operators can be applied to compare pointers of any type:
= = = < > <= >=
Comparison operators compare the addresses of two operands as if they were unsigned integers
class compareoperators{ unsafestaticvoid Main () { int 234; int 236 ; int* p1 = &x; int* P2 = &y; < p2);//true < p1);//false }}
C # Action Pointers