Illegal Arithmetic Operations : With Pointer
One can perform different arithmetic operations on Pointer such as increment,decrement but still we have some more arithmetic operations that cannot be performed on pointer -
- Addition of two addresses.
- Multiplying two addresses.
- Division of two addresses.
- Modulo operation on pointer.
- Cannot perform bitwise AND,OR,XOR operations on pointer.
- Cannot perform NOT operation or negation operation.
1. Addition of Two Pointers :
#include<stdio.h> int main() { int var = 10; int *ptr1 = &i; int *ptr2 = (int *)2000; printf("%d",ptr1+ptr2); return 0; }
Output :
Compile Error
2. Multiplication of Pointer and number :
#include<stdio.h> int main() { int var = 10; int *ptr1 = &i; int *ptr2 = (int *)2000; printf("%d",ptr1*var); return 0; }
Output :
Compile Error
3. Multiplication of Two Pointers :
#include<stdio.h> int main() { int var = 10; int *ptr1 = &i; int *ptr2 = (int *)2000; printf("%d",ptr1*ptr2); return 0; }
Output :
Compile Error
Similarly we cannot perform following operations -
4. Modulo Operation
int *ptr1 = (int *)1000; int *ptr2 = (int *)2000; int var = ptr2 % ptr1;
5. Division of pointer
#include<stdio.h> int main() { int *ptr1,*ptr2; ptr1 = (int *)1000; ptr2 = ptr1/4; return(0); }
Output :
6.Cannot perform bitwise OR
#include<stdio.h> int main(){ int i=5,j=10; int *p=&i; int *q=&j; printf("%d",p|q); return 0; }
7.Negation Operation on Pointer
#include<stdio.h> int main(){ int i=5; int *ptr=&i; printf("%d",~ptr); return 0; }
Summary at a glance :
Address + Address = Illegal Address * Address = Illegal Address / Address = Illegal Address % Address = Illegal Address & Address = Illegal Address | Address = Illegal Address ^ Address = Illegal ~Address = Illegal