Example
The prefixes ++ and * have the same precedence and are right to left-associative. Postfix ++ has higher precedence than prefix ++ and *and is associative from left to right. Look at the example below to see the difference between ++*p, *p++, and *++p.
Code:
#include <stdio.h>
int main() {
int nums[] = {10, 20, 30, 40, 50};
int *p = nums; //pointer storing the address of nums array
int var;
var = ++*p; //value at index 0 incremented by 1
printf("nums[0] = %d, nums[1] = %d, *p = %d, var = %d \n",
nums[0], nums[1], *p, var);
var = *p++; //value of index 0 stored in var and pointer get inc
printf("nums[0] = %d, nums[1] = %d, *p = %d, var = %d \n",
nums[0], nums[1], *p, var);
var = *++p; //pointer address get inc and then value is stored
printf("nums[0] = %d, nums[1] = %d, *p = %d, var = %d \n",
nums[0], nums[1], *p, var);
return 0;
}

You can also try this code with Online C Compiler
Run Code
Output:
nums[0] = 11, nums[1] = 20, *p = 11, var = 11
nums[0] = 11, nums[1] = 20, *p = 20, var = 11
nums[0] = 11, nums[1] = 20, *p = 30, var = 30
By recalling simple principles concerning postfix ++, prefix ++, and * (dereference) operators, the result of the programs mentioned above and all similar programs may be anticipated.
1) The prefixes ++ and * have the same precedence. Both have right-to-left associativity.
2) Postfix ++ has a greater precedence than both * and prefix ++. Postfix ++ associativity is left to right.
The compiler searches for associativity because ++*p contains two operators with the same precedence. Operator associativity is right to left. As a result, the expression is handled as ++(*p). As a result, the first program's output is "nums[0] = 11, nums[1] = 20, *p = 11."
Postfix ++ takes priority over *, the expression *p++ is evaluated as *(p++).
As a result, the second program's output is "nums[0] = 11, nums[1] = 20, *p = 20."
*++p contains two operators with the same precedence, the compiler searches for associativity. Operator associativity is right to left. As a result, the expression is interpreted as *(++p). As a result, the third program's output is "nums[0] = 11, nums[1] = 20, *p = 30."
Also See, Sum of Digits in C and Short int in C Programming
Frequently Asked Questions
What is the difference between i++ and ++i?
There are two types of ++ operators: pre and post-increment. i++ is post increment where the value of i will be used first and then increment. ++i is pre-increment, and it will first increment the value of i and then use it.
What is the dereference operator?
* operator is the dereference operator. Dereferencing a pointer occurs when this operator (*) is used with the pointer variable. When a pointer is dereferenced, the value of the variable pointed by the pointer is returned.
What is a pointer?
A pointer is an object that contains the address of a memory location i.e. the memory location's direct address. A pointer, like any other variable or constant, must be declared before it may be used to hold any variable address.
Conclusion
In this article, we have extensively discussed the difference between ++*p, *p++, and *++p in the C programming language.
We hope this blog has helped you enhance your knowledge regarding the pointers in the C language. Some official documentation on C programming language that can help you improve your understanding is C documentation.
Check out this problem - Longest Common Prefix
If you would like to learn more, check out our articles on pointers, learn pointers, and pointers in C.
Practice makes a man perfect. To practice and improve yourself in the interview, you can check out Top 100 SQL problems, Interview experience, Coding interview questions, and the Ultimate guide path for interviews.
Do upvote our blog to help other ninjas grow. Happy Coding!