"Original" learn C + + pointers--/++---------C + + Primer Plus (6th edition)

Source: Internet
Author: User

Speaking * and + + are used for pointers at the same time raised the question : What to dereference, what increment.

Double arr[5]={21.132.823.445.237.4  }; double *pt=arr;    // pt pointer pointing to arr[0] value is 21.1++pt;                  // pt pointer pointing to arr[1] value is     32.8

The right-to-left binding rule for the prefix operator means that

The meaning of *++p is as follows:

Apply + + to PT first (because + + is on the right of *) and then increment the * to the incremented pt-- pointer

Double // Pointer +1, point to arr[2] value is 23.4

The meaning of ++*pt is as follows:

means to get the value PT points to, and then increment the value by one--- point

// point to the value +1   23.4+1=24.4

(*pt) + + has the following meanings:

Parentheses indicate that the pointer is de-referenced first, resulting in 24.4. Then the operator + + increments the value to 25.4,pt still points to arr[2]

*p++ has the following meanings:

x=* pt++;

The postfix operator + + has a higher precedence, which means that the operator is used for PT instead of *PT, so the pointer is incremented.

The suffix operator, however, means that the original address (&arr[2]) is de-referenced instead of the incremented new address, so the value of *pt++ is arr[2], or 25.4,

However, the value of PT will be the address of arr[3] When the statement is executed.

#include"stdafx.h"#include<iostream>intMain () {using namespacestd; Doublearr[5]={21.1,32.8,23.4,45.2,37.4}; Double*pt=arr; ++pt; intA; Doublex; cout<<*++pt<<Endl; cout<<++*pt<<Endl; cout<< (*PT) ++<<Endl; cout<<*pt++<<Endl; X=*pt++; cout<<x<<Endl; CIN>>A;}

Note: The pointer increment and decrement follow the pointer's arithmetic rules, and if PT points to the first element of an array,++PT modifies the PT value (the first element)and points to the second element address (the second element value is unchanged)

"Original" learn C + + pointers--/++---------C + + Primer Plus (6th edition)

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.