Pointer Operation Summary

Source: Internet
Author: User

I. arithmetic operations:

1. addition and subtraction of pointer and Certificate: (PX + N, PX-N)

When a pointer is used as the address plus or minus an integer N, it means that the pointer is currently directed to the front or back of the nth data position. Since pointers can point to different data types, that is, data with different data lengths, the result of this operation depends on the Data Type pointed to by the pointer.

For example, when the character pointer is 1, the actual result is that the address value in the pointer is 1; the integer pointer is 1; the actual result is that the address value in the pointer is 2;

* The actual operations of P + N are: (p) + N * sizeof (data type );

* The actual operation of p-n is: (p)-N * sizeof (data type );

 

2. pointer addition 1 and subtraction 1: (PX ++, ++ PX, PX --, -- Px)

Has the Computing Features in 1, such as: Y = * PX ++;

There are three operations in this expression: =, *, and ++. * And ++ give priority to =. * And ++ are operations of the same level. The combination rule is from right to left. Therefore, the ++ operation is performed on PX. Equivalent to Y = * (PX ++ );

Here, PX ++ is a post operation. Therefore, the operation order of this expression is: access the target pointed to by the current PX value, assign the value of the target variable to Y, and then add Px to 1 to point to the next target.

 

3. subtraction of pointers: (PX-Py)

If the two pointers PX and Py point to the same type of variables, you can perform the subtraction operation on them. The result is the number of data points between the two pointer addresses. It is actually address computing. Therefore, the result of the two pointer Subtraction is not an address, but an integer.

 

Ii. relational operations:

The storage logic of data in the memory is from forward to backward, so the pointer to the back is greater than the pointer to the front.

 

Iii. Assignment operation:

1. Assign the address of a variable to a pointer pointing to the same data type, for example, char a, * P; P = &;

2. Assign the value of a pointer to another pointer of the same data type, for example, int * P, * q; P = Q;

3. Assign the array address a pointer to the same data type. Example: Char A [10], * pA; Pa =;

 

In addition, the pointer assignment is different. If it is defined at the beginning and assigned at the end, it is in the following form:

Char A, * P; P = &;

If the value is assigned at the beginning of the definition, the format is as follows:

Int A, * P = &;

So we can understand this form:

Int N; int * P1 = & N; int * P2 = p1; Because P1 is only * P1 = & n when defining the value assignment, if P1 is used when it is not defined, then P1 represents & N and does not need to be written as * P1;

The above form is equivalent to: int N; int * P1 = & N; int * P2 = & N;

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.