0% found this document useful (0 votes)
18 views8 pages

Pointer Arithmetics in C With Examples

Pointer arithmetic in C allows operations like incrementing, decrementing, adding or subtracting integers to/from pointers, and comparing pointers. Each operation adjusts the pointer's address based on the size of the data type it points to. Examples illustrate how these operations work with integer, float, and char pointers.

Uploaded by

Shahla Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views8 pages

Pointer Arithmetics in C With Examples

Pointer arithmetic in C allows operations like incrementing, decrementing, adding or subtracting integers to/from pointers, and comparing pointers. Each operation adjusts the pointer's address based on the size of the data type it points to. Examples illustrate how these operations work with integer, float, and char pointers.

Uploaded by

Shahla Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Pointer Arithmetic’s in C with Examples

Pointer Arithmetic is the set of valid arithmetic operations that can be performed
on pointers. The pointer variables store the memory address of another variable.
It doesn’t store any value.
Hence, there are only a few operations that are allowed to perform on Pointers in
C language. The C pointer arithmetic operations are slightly different from the
ones that we generally use for mathematical calculations. These operations are:

1. Increment/Decrement of a Pointer
2. Addition of integer to a pointer
3. Subtraction of integer to a pointer
4. Subtracting two pointers of the same type
5. Comparison of pointers
1. Increment/Decrement of a Pointer
Increment: It is a condition that also comes under addition. When a pointer is
incremented, it actually increments by the number equal to the size of the data
type for which it is a pointer.
For Example:
If an integer pointer that stores address 1000 is incremented, then it will
increment by 4(size of an int), and the new address will point to 1004. While if a
float type pointer is incremented then it will increment by 4(size of a float) and
the new address will be 1004.
Decrement: It is a condition that also comes under subtraction. When a pointer is
decremented, it actually decrements by the number equal to the size of the data
type for which it is a pointer.
For Example:
If an integer pointer that stores address 1000 is decremented, then it will
decrement by 4(size of an int), and the new address will point to 996. While if a
float type pointer is decremented then it will decrement by 4(size of a float) and
the new address will be 996.

Example of Pointer Increment and Decrement

Below is the program to illustrate pointer increment/decrement:

#include <stdio.h>
// pointer increment and decrement
//pointers are incremented and decremented by the size of the data type they point
to
int main()
{
int a = 22;
int *p = &a;
printf("p = %u\n", p); // p = 6422288
p++;
printf("p++ = %u\n", p); //p++ = 6422292 +4 // 4 bytes
p--;
printf("p-- = %u\n", p); //p-- = 6422288 -4 // restored to original value
float b = 22.22;
float *q = &b;
printf("q = %u\n", q); //q = 6422284
q++;
printf("q++ = %u\n", q); //q++ = 6422288 +4 // 4 bytes
q--;
printf("q-- = %u\n", q); //q-- = 6422284 -4 // restored to original value
char c = 'a';
char *r = &c;
printf("r = %u\n", r); //r = 6422283
r++;
printf("r++ = %u\n", r); //r++ = 6422284 +1 // 1 byte
r--;
printf("r-- = %u\n", r); //r-- = 6422283 -1 // restored to original value
return 0;
}

2. Addition of Integer to Pointer


When a pointer is added with an integer value, the value is first multiplied by the
size of the data type and then added to the pointer.

For Example:
Consider the same example as above where the ptr is an integer pointer that
stores 1000 as an address. If we add integer 5 to it using the expression, ptr = ptr
+ 5, then, the final address stored in the ptr will be ptr = 1000 + sizeof(int) * 5 =
1020.

Example of Addition of Integer to Pointer

// C program to illustrate pointer Addition


#include <stdio.h>
int main()
{
// Integer variable
int N = 4;
// Pointer to an integer
int *ptr1, *ptr2;
// Pointer stores the address of N
ptr1 = &N;
ptr2 = &N;
printf("Pointer ptr2 before Addition: ");
printf("%p \n", ptr2);
// Addition of 3 to ptr2
ptr2 = ptr2 + 3;
printf("Pointer ptr2 after Addition: ");
printf("%p \n", ptr2);
return 0;
}
3. Subtraction of Integer to Pointer
When a pointer is subtracted with an integer value, the value is first multiplied by
the size of the data type and then subtracted from the pointer similar to addition.

For Example:
Consider the same example as above where the ptr is an integer pointer that
stores 1000 as an address. If we subtract integer 5 from it using the
expression, ptr = ptr – 5, then, the final address stored in the ptr will be ptr =
1000 – sizeof(int) * 5 = 980.

Example of Subtraction of Integer from Pointer

Below is the program to illustrate pointer Subtraction:

// C program to illustrate pointer Subtraction


#include <stdio.h>
int main()
{
// Integer variable
int N = 4;
// Pointer to an integer
int *ptr1, *ptr2;
// Pointer stores the address of N
ptr1 = &N;
ptr2 = &N;
printf("Pointer ptr2 before Subtraction: ");
printf("%p \n", ptr2);
// Subtraction of 3 to ptr2
ptr2 = ptr2 - 3;
printf("Pointer ptr2 after Subtraction: ");
printf("%p \n", ptr2);
return 0;
}
4. Subtraction of Two Pointers
The subtraction of two pointers is possible only when they have the same data
type. The result is generated by calculating the difference between the addresses
of the two pointers and calculating how many bits of data it is according to the
pointer data type. The subtraction of two pointers gives the increments between
the two pointers.

For Example:
Two integer pointers say ptr1(address:1000) and ptr2(address:1004) are
subtracted. The difference between addresses is 4 bytes. Since the size of int is 4
bytes, therefore the increment between ptr1 and ptr2 is given by (4/4) = 1.

Example of Subtraction of Two Pointer

Below is the implementation to illustrate the Subtraction of Two Pointers:

// C program to illustrate Subtraction


// of two pointers
#include <stdio.h>
int main()
{
int x = 6; // Integer variable declaration
int N = 4;
// Pointer declaration
int *ptr1, *ptr2;
ptr1 = &N; // stores address of N
ptr2 = &x; // stores address of x
printf(" ptr1 = %u, ptr2 = %u\n", ptr1, ptr2);
// %p gives an hexa-decimal value,
// We convert it into an unsigned int value by using %u
// Subtraction of ptr2 and ptr1
x = ptr1 - ptr2;
// Print x to get the Increment
// between ptr1 and ptr2
printf("Subtraction of ptr1 "
"& ptr2 is %d\n",
x);
return 0;
}
5. Comparison of Pointers
We can compare the two pointers by using the comparison operators in C. We
can implement this by using all operators in C >, >=, <, <=, ==, !=. It returns
true for the valid condition and returns false for the unsatisfied condition.
1. Step 1: Initialize the integer values and point these integer values to the
pointer.
2. Step 2: Now, check the condition by using comparison or relational operators
on pointer variables.
3. Step 3: Display the output.

Example of Pointer Comparision

// C Program to illustrare pointer comparision


#include <stdio.h>
int main()
{
// declaring array
int arr[5];
// declaring pointer to array name
int* ptr1 = &arr;
// declaring pointer to first element
int* ptr2 = &arr[0];
if (ptr1 == ptr2) {
printf("Pointer to Array Name and First Element "
"are Equal.");
}
else {
printf("Pointer to Array Name and First Element "
"are not Equal.");
}
return 0;
}

You might also like