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

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

醉折花枝作酒筹
醉折花枝作酒筹Original
2021-07-09 11:47:2256425browse

The difference between "*p" and "(*p)" is: 1. "*p" indicates the value of the unit pointed to by p, and p points to the next unit, that is, p increases by 1. "*p" refers to the next address. 2. "(*p)" means adding one to the value of the data pointed to by *p.

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

The operating environment of this tutorial: Windows 7 system, C 17 version, Dell G3 computer.

Difference:

1. *p indicates the value of the unit pointed to by p, and p points to the next unit, that is, p increases by 1. *p refers to the next address.

2. (*p) means adding one to the value of the data pointed to by *p.

The C compiler considers * and to be operators of the same priority, and they are combined from right to left, so *p only acts on p, which has the same meaning as *(p); in ( *p), since () has a higher priority than * and, it acts on the expression *p within ().

For example:

int x,y,a[]={1,2,3,4,5},*p=a,*q=a;

x=*p ;//After executing this sentence, x=a[0]=1, p=a 1

y=(*q);//After executing this sentence, y=a[ 0] 1=2, q still=a

Extended information:

Verification program 1

#include"stdio.h"
void main()
{
int a=2;
int*p=&a;
int*pold;//记录P指向的地址,为了作为比较使用
pold=p;
//-----------原来的信息
printf("原来的a=%d",a);
printf("原来的p=%x",p);
printf("原来的pold=%x",pold);
//----------进行变化
printf("*p++的结果=%d,a的结果a=%d",*p++,a);
printf("地址变化的结果p-pold=%x",p-pold);
}

Program 2

#include"stdio.h"
void main()
{
int a=2,*p=&a,*q=&a;
printf("%d%d",(*p),a);
printf("%d%d%d%d",(*p)++,*p++,*q++,a);
printf("%d",a);
}

Procedure 3:

#include"stdio.h"
void main()
{
int a=2,*p=&a,*q=&a;
printf("%d%d",(*p),a);
printf("%d%d%d",*p++,(*p)++,*q++);
p=&a;q=&a;
printf("%d%d%d%d",a,*p,(*q)++,a);
printf("%d%d%d%d",a,++(*p),++(*q),a);
printf("%d",a);
}

To sum up:

*p is to take out the value of *p first, and then let p

(*p) is to take out the value of *p first , let this value

*(P) be to take out the value of *p first, let p

So, *p is equivalent to *(P)

and printf The running order is from right to left. Moreover, the right is executed after the entire operation expression is calculated, and the execution order of the right is from left to right. When left encounters a variable, it will immediately increase the value of the variable.

Verify the order of operations of printf

#include"stdio.h"
void main()
{
int a=2;
printf("a++=%d,++a=%d,a++=%d",a++,++a,a++);
printf("a=%d",a);
}

Recommended tutorial: "C#"

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