Arithmetic Operation With Pointer in C Programming
Arithmetic Operation With Pointer in C Programming
Arithmetic Operation With Pointer in C Programming
Rule 1:
Address + Number= Address
Address - Number= Address
Address++ = Address
Address-- = Address
++Address = Address
--Address = Address
If we will add or subtract a number from an address
result will also be an address.
New address will be:
void main(){
int *ptr=( int *)1000;
ptr=ptr+1;
printf(" %u",ptr);
Output: 1002
(2)What will be output of following c program?
void main(){
double *p=(double *)1000;
p=p+3;
printf(" %u",p);
Output: 1024
void main(){
float array[5]={1.1f,2.2f,3.3f};
float(*ptr)[5];
ptr=&array;
printf("%u",ptr);
ptr=ptr+1;
printf(" %u",ptr);
typedef struct abc{
int far*a;
double b;
unsigned char c;
}ABC;
void main(){
ABC *ptr=(ABC *)1000;
ptr=ptr+2;
printf(" %u",ptr);
Output: 1026
Output: 960
float * display(int,int);
int max=5;
void main(){
float *(*ptr)(int,int);
ptr=display;
(*ptr)(2,2);
printf("%u",ptr);
ptr=ptr+1;
printf(" %u",ptr);
}
float * display(int x,int y){
float f;
f=x+y+max;
return &f;
}
Rule 2:
Address - Address=Number
void main(){
float *p=(float *)1000;
float *q=(float *)2000;
printf("Difference= %d",q-p);
}
Output: Difference= 250
Explanation:
q-p=(2000-100)/sizeof(float)
=1000/4
=250
struct abc{
signed char c;
short int i;
long double l;
};
void main(){
struct abc *p,*q;
p=(struct abc *)1000;
q=(struct abc *)2000;
printf("Difference= %d",q-p);
}
Output: Difference= 76
Explanation:
q-p=(2000-1000)/sizeof(struct abc)
=1000/(1+2+10)
=1000/13
=76
typedef union xxx{
char far * c;
const volatile i;
long int l;
}XXX;
void main(){
XXX *p,*q;
p=(XXX *)1000;
q=(XXX *)2000;
printf("Difference= %d",q-p);
}
Output: Difference= 250
Explanation:
q-p=(2000-100)/max(4,2,4)
=1000/4
=250
void main(){
const volatile array[4]={0};
const volatile(*p)[4]=&array;
const volatile(*q)[4]=&array;
q++;
q++;
printf("%u %u\n",p,q);
printf("Difference= %d",q-p);
Rule 3:
Address + Address=Illegal
Address * Address=Illegal
Address / Address=Illegal
Address % Address=Illegal
void main(){
int i=5;
int *p=&i;
int *q=(int *)2;
printf("%d",p+q);
}
Output: Compiler error
void main(){
int near*p=(int near*)0x0A0005555;
int near*q=(int near*)0x0A2115555;
if(p==q)
printf("Equql");
else
printf("Not equal");
Output: Equal
void main(){
int far*p=(int far*)0x0A0005555;
int far*q=(int far*)0x0A2115555;
if(p==q)
printf("Equql");
else
printf("Not equal");
Output: Not equal
c. If two pointers are huge pointer it will first
normalize into the 20 bit actual physical address
and compare to its physical address.
void main(){
int huge*p=(int huge*)0x0A0005555;
int huge*q=(int huge*)0x0A2113445;
if(p==q)
printf("Equql");
else
printf("Not equal");
Output: Equal
Rule 5: We can perform bitwise operation between
two pointers like
void main(){
int i=5,j=10;
int *p=&i;
int *q=&j;
printf("%d",p|q);
Output: Compiler error
void main(){
int near*far*huge* p;
printf("%d",sizeof(p));
printf(" %d",sizeof(*p));
printf(" %d",sizeof(**p));
Output: 4 4 2