++ * p ++ does'nt work on the second element in an array?

#include<stdio.h>

int main()
{
   int *p;
   int arr[]={10,20};
   p=arr;
   ++*p; //expected evaluation = (++(*p))
   printf("arr[0]=%d, arr[1]= %d, p=%d\n",arr[0],arr[1],*p);
}

Conclusion = arr[0]=11, arr[1]= 20, p=11which is completely beautiful. But here

#include<stdio.h>

int main()
{
   int *p;
   int arr[]={10,20};
   p=arr;
   ++*p++; //expected evaluation = (++(*(p++))) = *p->20 , thus ++20 = 21
   printf("arr[0]=%d, arr[1]= %d, p=%d\n",arr[0],arr[1],*p);
}

Since the priority of postfix is ++higher, the value a[1]or *pshould be 21, but there is:

arr[0]=11, arr[1]= 20, p=20

For some reason, does it not increase? Why?

+4
source share
2 answers

Following the priority of the operator and the order of evaluation, the operator ++*p++first writes the increment of the pointer p, since the message The increment operator -fix has a higher priority than dereferencing.

p++ p p, 20. postfix ( arr[0]) 10, , , 11.

, {11,20}, .

+3
++*p++;

:

int* p1 = p++; // p1  points to a[0]. p points to a[1]
++(*p1);       // Increments a[0]

.

0

Source: https://habr.com/ru/post/1598313/


All Articles