P * P & amp; P, * P

Source: Internet
Author: User

P * P & P, * P
Zookeeper

Background Summary

 

I attended the group C ++ seminar last night, which involves many aspects, including this pointer, heavy-duty operator function, array pointer and pointer array, static member static, and so on. When we talk about the knowledge points of * P and P, it gets stuck...

 

Starting from preparing C ++, we have generally reflected that * P and P are a bit confusing. Sometimes, it may happen to be correct, and changing the data won't happen again... The pointer is required to learn C ++. Today, I want to explain my understanding to you. Although the exam is coming soon, I still hope to help students who need it, correction is not appropriate.

 

P * P & P

 

To identify these expressions, you need to understand their meanings.

Before that, let's look at the following table in detail:

 

 

Expression

Int

Int * P

Type

Int

Int *

Variable

A

P

 

P, pointer variable. To learn the pointer, we first understand that the pointer stores the address pointing to the bucket. Simply put, the pointer variable stores the address, so P represents the address, the house number.

 

* P: value. The actual content of the address space memory that the P Pointer Points to is obtained, that is, the person in the house with the specified house number.

 

& P is an address. It is easy to understand that it is an address, so & P gets an address and a house number.

 

The following is an example:

Int a [5] = {1, 2, 3, 4, 5}, * P = a. Now I want to get the value 4, which has the following expressions:

A) * P + 4 B) * (P + 3) C) P + 3

 

Next, let's take a look at the meaning of each expression:

 

A, * P, value, and get the storage content of the first address of the bucket pointed to by P. Here is 1, so * P + 4 = 5, the option is incorrect.

B, * P, value; P, representing the address. Now P points to 20, so P + 3 = 23, which is equivalent to moving the pointer down three storage units and now pointing to address 23, so * (P + 4) = 4.

C. I have learned from option B that P + 3 = 23. It means the address, so C is incorrect.

 

The above is my understanding of the pointer. I am about to take the test. I wish you all the tests will pass!


What are the similarities and differences between C Language * p ++ and (* p) ++ and * (p ++?

* P ++ is equivalent to * (p ++), indicating to take the value of the unit referred to by p. p points to the next unit, that is, p automatically adds 1.
(* P) ++ indicates taking the value of the unit referred to by p. The value of this unit is incremented by 1.

// The following main test program and running status.
# Include <stdio. h>
Void main ()
{
Int a [] = {1, 1, 1}, * p;
P =;
* P ++;
Printf ("p = % d * p = % d \ n", p, * p );
P =;
(* P) ++;
Printf ("p = % d * p = % d \ n", p, * p );
}

// Program running status:
P = 1439896 * p = 1
P = 1439892 * p = 2
Press any key to continue...

* P ++, (* p) ++, * ++ p, ++ * p are different?

For example

Int a [5] = {1, 2, 3, 4, 5 };
Int * p =;

* P ++ first obtains the value pointed to by the pointer p (the first element 1 of the array), and then increases the pointer p by 1;

Cout <* p ++; // The result is 1.

Cout <(* p ++); // 1

(* P) ++ first goes to the value pointed to by the pointer p (the first element 1 of the array), and then increases the value by 1 (the first element of the array is changed to 2 ).
Cout <(* p) ++; // 1
Cout <(* p) ++) // 2
* ++ P first increments the pointer p by 1 (pointing to the second element of the array at this time), and * retrieves the value.

Cout <* ++ p; // 2
Cout <(* ++ p) // 2

+ * P first obtains the value pointed to by the pointer p (the first element 1 of the array), and then increases the value by 1 (the first element of the array is changed to 2)
Cout <++ * p; // 2
Cout <(++ * p) // 2

Note that each cout output above must be output separately to obtain the subsequent results.

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.