+ 9

Why this code prints CA ?

char a[]={'A', 'B', 'C', 'D'}; char *ppp = &a[0]; *ppp++; printf("%c%c", *++ppp, --*ppp);

27th Jan 2019, 6:18 AM
Mihai C
Mihai C - avatar
5 ответов
+ 4
I didn't understand how it's work at this line: printf("%c%c", *++ppp, --*ppp); // As I see *++ppp should change the point to the next point a[2] and return its value. --*ppp should get current point and decrease its value. Why the previous value is changing a[1] and not the current a[2]??? If we add printf of *ppp to the end it will return a[2].
27th Jan 2019, 10:43 AM
Earl_Grey
Earl_Grey - avatar
+ 4
Earl Grey, you're absolutely right. It applies right to left rule.
27th Jan 2019, 3:54 PM
Nilesh Ingale
Nilesh Ingale - avatar
+ 3
char a[]={'A', 'B', 'C', 'D'}; char *ppp = &a[0]; *ppp++; //This changes the pointer to point to a[1] printf("%c%c", *++ppp, --*ppp); //equal precedence so right to left rule
27th Jan 2019, 6:24 AM
Aadhish Sriram
Aadhish Sriram - avatar
+ 3
Let start from the first *ppp++; which is before the printf statement which will take it to 'B', and in the printf moving a step ahead is 'C' and taking a step backward is 'A'. I hope you get that Mihai C
28th Jan 2019, 7:06 AM
✳AsterisK✳
✳AsterisK✳ - avatar
+ 3
After assigning the 0th Element the pointer having value of a[0]... Then you increment the pointer .... That will get the 1st Element and after that.... In printf, if you increment it will go 2nd position and if you decrement it will go 0th position.... But actually the pointer in 1st position.... That's all..
29th Jan 2019, 1:40 AM
GOBIKRISHNAN R
GOBIKRISHNAN R - avatar