Pointer Increment & Decrement in C
Notes:
Pointer Increment & Decrement Operations in C Programming Language:
3. Increment and decrement operations: pointer arithmetic in c
To navigate from one memory location to another memory location of the same type sequentially; we can increment or decrement a pointer variable value by 1.
W.K.T. To increment or decrement any variable value by 1 we take help of increment or decrement operators.
++ : Increment Operator : when used; it increments a variable value by 1
-- : Decrement Operator : when used; it decrements a variable value by 1
Note: while working with pointer variables; the amount of value incremented or decremented depends upon the size of pointer variable type
Ex 1: int *iptr;
iptr++; // it increments iptr value by 4 because size of the int type is 4 bytes
iptr points to Nextint type memory location ; which is 4 bytes away from the current memory location
iptr--; // it decrements iptr value by 4 because size of the int type is 4 bytes
iptr points to Previousint type memory location ; which is 4 bytes away from the current memory location
Ex 2: short int *sptr;
sptr++; // it increments sptr value by 2 because size of the short int type is 2 bytes
sptr points to Nextshort int type memory location ; which is 2 bytes away from the current memory location
sptr--; // it decrements sptr value by 2 because size of the short int type is 2 bytes
sptr points to Previousshort int type memory location ; which is 2 bytes away from the current memory location
Ex 3: char *cptr;
cptr++; // it increments cptr value by 1 because size of the char type is 1 byte
cptr points to Nextchar type memory location ; which is 1 byte away from the current memory location
cptr--; // it decrements cptr value by 1 because size of the char type is 1 byte
cptr points to Previouschar type memory location ; which is 1 byte away from the current memory location
Example Code:
#include <stdio.h>
int main()
{
int numbers[5] = {1,2,3,4,5};
int *iptr = &numbers[0]; // 2293512
printf("Value of iptr is=%d\n",iptr); // 2293512
printf("Value at iptr is pointing=%d\n",*iptr); // 1
printf("\n");
iptr++; // 2293516
printf("Value of iptr is=%d\n",iptr); // 2293516
printf("Value at iptr is pointing=%d\n",*iptr); // 2
printf("\n");
iptr++; // 2293520
printf("Value of iptr is=%d\n",iptr); // 2293520
printf("Value at iptr is pointing=%d\n",*iptr); // 3
printf("\n");
iptr++; // 2293524
printf("Value of iptr is=%d\n",iptr); // 2293524
printf("Value at iptr is pointing=%d\n",*iptr); // 4
printf("\n");
iptr++; // 2293528
printf("Value of iptr is=%d\n",iptr); // 2293528
printf("Value at iptr is pointing=%d\n",*iptr); // 5
printf("\n");
iptr--; // 2293524
printf("Value of iptr is=%d\n",iptr); // 2293524
printf("Value at iptr is pointing=%d\n",*iptr); // 4
printf("\n");
iptr--; // 2293520
printf("Value of iptr is=%d\n",iptr); // 2293520
printf("Value at iptr is pointing=%d\n",*iptr); // 3
printf("\n");
iptr--; // 2293516
printf("Value of iptr is=%d\n",iptr); // 2293516
printf("Value at iptr is pointing=%d\n",*iptr); // 2
printf("\n");
iptr--; // 2293512
printf("Value of iptr is=%d\n",iptr); // 2293512
printf("Value at iptr is pointing=%d\n",*iptr); // 1
printf("\n");
return 0;
}