Home  >  Article  >  What is the difference between *p++ and (*p)++

What is the difference between *p++ and (*p)++

尊渡假赌尊渡假赌尊渡假赌
尊渡假赌尊渡假赌尊渡假赌Original
2023-05-23 11:48:198733browse

The difference between *p and (*p) is: 1. *p is a combination of postfix increment operators, (*p) is a postfix increment operator applied to *p expression; 2. *p The expression first returns the value pointed to by "p" and then increments "p", (*p) returns the value pointed to by "p" and then increments that value.

What is the difference between *p++ and (*p)++

# Operating system for this tutorial: Windows 10 system, C18 version, Dell G3 computer. The difference between

*p and (*p)

*p and (*p) are two different expressions:

  1. *p: This is a combination of postfix increment operators. First, the *p expression will dereference the pointer p and return the value pointed to by the pointer. Then, the pointer p is incremented and points to the next element. Therefore, this expression first returns the value pointed to by p and then increments p.

  2. (*p) : This is a postfix increment operator applied to the *p expression. First (*p) dereferences the pointer p and returns the value pointed to by the pointer. Then, the value is incremented. Therefore, this expression first returns the value pointed to by p and then increments that value.

Here is a sample code to illustrate the difference between the two:

#include <iostream>
int main() {
int arr[] = {1, 2, 3};
int* p = arr;
std::cout << *p++ << std::endl;    // 输出: 1
std::cout << *p << std::endl;      // 输出: 2
p = arr;
std::cout << (*p)++ << std::endl;  // 输出: 1
std::cout << *p << std::endl;      // 输出: 2
return 0;
}

In the above example, *p outputs p first The value pointed to is 1, then p is incremented to the next element, and now p points to 2. And (*p) first outputs the value pointed to by p, 1, and then increments the value. Now the value pointed by p is 2. This shows the difference between the two.

The above is the detailed content of What is the difference between *p++ and (*p)++. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn