what is the use of type void and and in which type of pointers arithmatic is not permitted
Doubt in mcq pointers quesion 2
@chronic.geeks The data type void actually refers to an object that does not have a value of any type. We have already seen examples of its use when we have defined functions that return no value, i.e. functions which only print a message and have no value to return. Such a function is used for its side effect and not for its value
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.
- 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 –
- 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);
}
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
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.