C Programming language

adplus-dvertising
Pointer in C: Pointer expression and pointer airthmetic
Previous Home Next

Pointer expression and pointer airthmetic

Like other variables pointer variables can be used in expressions. For example if p1 and p2 are properly declared and initialized pointers, then the following statements are valid.

y=*p1**p2; 
sum=sum+*p1; 
z= 5* - *p2/p1; 
*p2= *p2 + 10;

C allows us to add integers to or subtract integers from pointers as well as to subtract one pointer from the other. We can also use short hand operators with the pointers p1+=; sum+=*p2; etc.

we can also compare pointers by using relational operators the expressions such as p1 >p2 , p1==p2 and p1!=p2 are allowed.

/*Program to illustrate the pointer expression and pointer arithmetic*/ 
#include< stdio.h > 
main() 
{ 
int ptr1,ptr2; 
int a,b,x,y,z; 
a=30;b=6; 
ptr1=&a; 
ptr2=&b; 
x=*ptr1+ *ptr2 –6; 
y=6*- *ptr1/ *ptr2 +30; 
printf(“\nAddress of a +%u”,ptr1); 
printf(“\nAddress of b %u”,ptr2); 
printf(“\na=%d, b=%d”,a,b); 
printf(“\nx=%d,y=%d”,x,y); 
ptr1=ptr1 + 70; 
ptr2= ptr2; 
printf(“\na=%d, b=%d”,a,b); 
}

Some other points related to function are given below:

  1. If we want that the value of an actual argument should not get changed in the function being called, pass the actual argument by value.
  2. If we want that the value of an actual argument should get changed in the function being called, pass the actual argument by reference.
  3. If a function is to be made to return more than one value at a time then return these values indirectly by using a call by reference.
Previous Home Next