#include <stdio.
h>
int main()
{
int m = 100;
int *ptr = &m;
printf("The Value of Variable m is: %d\n", m);
printf("The Memory Address of Variable m is: %p\n", &m);
printf("The Memory Address of Variable m is using ptr: %p\n", ptr);
return 0;
}
#include <stdio.h>
int main() {
int a = 10;
int * ptr;
ptr = &a;
printf("ptr = %p\n", ptr);
printf("*ptr = %d", *ptr);
return 0;
}
#include <stdio.h>
int main() {
char *s = "welcome";
printf("%s", s);
return 0;
}
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.
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.
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.
#include <stdio.h>
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:
ptr = 123400
ptr = ptr + 1
ptr = ptr + sizeof(int)*1
ptr = 123400 + 4
ptr = 123404
include<stdio.h>
int main(){
int number=50;
int *p;//pointer to int
p=&number;//stores the address of number variable
printf("Address of p variable is %u \n",p);
p=p+3; //adding 3 to pointer variable
printf("After adding 3: Address of p variable is %u \n",p);
return 0; }
Address of p variable is 137825204
After adding 3: Address of p variable is 137825216
Pointer Subtraction
we can subtract a value from the pointer variable. Subtracting any number from a pointer
will give an address.
new_address= current_address - (number * size_of(data type))
#include<stdio.h>
int main(){
int number=50;
int *p;//pointer to int
p=&number;//stores the address of number variable
printf("Address of p variable is %u \n",p);
p=p-3; //subtracting 3 from pointer variable
printf("After subtracting 3: Address of p variable is %u \n",p);
return 0;
}
Address of p variable is 3214864300
After subtracting 3: Address of p variable is 3214864288
Subtraction of Two Pointers
When you subtract two pointers that point to the same data type, the result is calculated by
finding the difference between their addresses and then dividing by the size of the data type
to determine how many elements (not bytes) separate the two pointers.
You have two integer pointers, ptr1 with an address of 1000 and ptr2 with an
address of 1004.
When you subtract these two pointers (ptr2 - ptr1), you get a result of 4 bytes, which
is the difference in addresses.
Since the size of an integer is 4 bytes, you divide the address difference by the size of
the data type: (4 / 4), which equals 1. This means there is an increment of 1 integer-
sized element between ptr1 and ptr2.
#include <stdio.h>
void main()
{
int x = 6; // Integer variable declaration
int N = 4;
int *ptr1, *ptr2;
ptr1 = &N; // stores address of N
ptr2 = &x; // stores address of x
printf(" ptr1 = %x, ptr2 = %x\n", ptr1, ptr2);
x = ptr1 - ptr2;
printf("Subtraction of ptr1 and ptr2 is %d\n", x);
}
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.
// C Program to illustrare pointer comparision
#include <stdio.h>
void main()
{
int arr[5];
int* ptr1;
ptr1 = &arr;
int* ptr2;
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."); }
}
Pointer to Array Name and First Element are Equal.
Comparison to NULL
A pointer can be compared or assigned a NULL value irrespective of what is the pointer
type. Such pointers are called NULL pointers .
// C Program to demonstrate the pointer comparison with NULL
// value
#include <stdio.h>
int main()
{
int* ptr = NULL;
if (ptr == NULL) {
printf("The pointer is NULL");
}
else {
printf("The pointer is not NULL");
}
return 0;
}
// Pointer Comparision in Array
#include <stdio.h>
int main()
{
int n = 10; // length of an array
int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int* ptr; // Declaration of pointer variable
ptr = arr; // Pointer points the first (0th index)
// element in an array
int count_even = 0;
int count_odd = 0;
for (int i = 0; i < n; i++) {
if (*ptr % 2 == 0) {
count_even++;
}
if (*ptr % 2 != 0) {
count_odd++;
}
ptr++; // Pointing to the next element in an array
}
printf("No of even elements in an array is : %d",
count_even);
printf("\nNo of odd elements in an array is : %d",
count_odd);
}
No of even elements in an array is : 5
No of odd elements in an array is : 5