Skip to main content

C pointers with pre-increment and post-increment operators


Pointers with pre-increment and post-increment operators is one of the confusing topics for the new learners. In this post, I'll try to explain the concept with an example.

I'll use the below code example to explain the concept.
#include<stdio.h>
#include<stdlib.h>
int main()
{
	int x;
	int *xp;
	xp = &x;
	x = 5;
	printf("Original Value of variable x is %d\n",x);
	printf("Value of x with post-increment operator is %d\n",x++);
	printf("Value of x after post-increment operator is %d\n",x);
	x = 5;
	printf("Original Value of variable x is %d\n",x);
	printf("Value of x with pre-increment operator is %d\n",++x);
	printf("Value of x after pre-increment operator is %d\n",x);
	x = 5;
        printf("\nOriginal Value of variable x is %d\n",x);
	printf("Value of x with post-increment operator called on pointer is %d\n",*xp++);
	printf("Value of x after post-increment operator called on pointer is %d\n",*xp);
	x = 5;
        xp = &x;
        printf("Original Value of variable x is %d\n",x);
	printf("Value of x with pre-increment operator called on pointer is %d\n",*++xp);
	printf("Value of x after pre-increment operator called on pointer is %d\n",*xp);
	x = 5;
        xp = &x;
        printf("\nOriginal Value of variable x is %d\n",x);
	printf("Value of x with post-increment operator called on pointer is %d\n",(*xp)++);
	printf("Value of x after post-increment operator called on pointer is %d\n",*xp);
	x = 5;
        xp = &x;
        printf("\nOriginal Value of variable x is %d\n",x);
	printf("Value of x with pre-increment operator called on pointer is %d\n",++(*xp));
	printf("Value of x after pre-increment operator called on pointer is %d\n",*xp);
	return 0;
}

Initial statements in the program just declare an integer variable and a pointer to an integer variable.

Post increment operator i++, in general is evaluated as
- return the value of i and
- then increment the value of i by 1

Pre increment operator ++i, on the other hand is evaluated as
- increment the value of i by 1 and then
- return the value of i

Based on the above concept, in the above example, results for post increment operator will be as below. As mentioned, first call to x++ will return the original value of x, which will be 5 in our case and then it will increment x. So, the next call to print the value of x will print an updated value of 6.

Original Value of variable x is 5
Value of x with post-increment operator is 5
Value of x after post-increment operator is 6

For pre-increment operator, value of x is incremented first and then printed even in the first call, so the printf statements will result in 6

Original Value of variable x is 5
Value of x with pre-increment operator is 6
Value of x after pre-increment operator is 6
Lets understand what the statement *xp++ means. Actually, if you look at the precedence table, post increment operator ++ takes precedence over derefernce * operator. So, as a result,  the *xp++ will be evaluated as *(xp++)

Now, xp is the pointer to variable x i.e. it is holding the address of variable x and we can reference its value by *xp. Based on the post increment operator concept, xp++ will return the value of xp and then increment its value i.e. it will return the original address of variable x and then increment the address. Now dereference operator comes into picture i.e. * operator will now be used to retrieve the value from pointer address, which in our case will be the actual address of variable x (non-incremented original address).

In our example, the first call to *xp++ will give the value of variable x since xp is holding the address of variable x. Because of post increment operator, the pointer address will be incremented and the next statement of *xp will try to access the value stored at a different address (undefined address). Depending upon your operating system and compiler, you will get undefined value. I am using GCC, so get the value of 0.

Original Value of variable x is 5
Value of x with post-increment operator called on pointer is 5
Value of x after post-increment operator called on pointer is 0

Lets now see the pre-increment operator statement with pointer i.e. *++xp. Looking at the precedence table again, pre-increment operator and dereferene operator will have same precedence. So, in this case, since both the operators have same precedence, associativity will come into picture which for our case is right to left. So *++xp will be evaluated as *(++xp). It means, first the value of ++xp will be evaluated which will increment the pointer address and return that. Then dereference operator will try to access that value. Since, we haven't stored any value at that pointer address, it will result in 0.

Original Value of variable x is 5
Value of x with pre-increment operator called on pointer is 0
Value of x after pre-increment operator called on pointer is 0

To get the results as seen in our original usage of pre increment and post increment operator, pointer variable should be used as (*xp)++ for post increment and ++*xp or ++(*xp) for pre-increment operators

Original Value of variable x is 5
Value of x with post-increment operator called on pointer is 5
Value of x after post-increment operator called on pointer is 6
Original Value of variable x is 5
Value of x with pre-increment operator called on pointer is 6
Value of x after pre-increment operator called on pointer is 6

Comments