C # Action Pointers

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.