Pointer-4 Pointer in Expresson

Pointers in Expression :

Pointers can also be used in an expression. While using in an expression it must be enclosed into a parenthesis to avoid errors. For example,

/* Wap to add two numbers */

 # include < stdio.h >
main( )
{
int a,b,c;
int *p,*q,*r;
p = &a;
q = &b;
r = &c;

printf ( " \n Enter the value for a and b " );
scanf ( "%d%d",p,q );
*r = ( *p ) +( *q );
printf ( " \n a=%d\nb=%d\nc=%d",*p,*q,*r );
}

In the above program, 3 variables namely a,b,c are declared and 3 pointers namely p,q,r are declared. Then addresses of a,b,c are stored into pointers p,q,r respectively. After accepting input values for a and b variable the equation,

*r = ( *p ) + ( *q );

shows that pointer variables are enclosed into a parenthesis. If we don't enclose pointers in this situation it works like *r =  *p  +  *q. Suppose the equation is c=a/b. If we want to represent this equation using pointer without parenthesis in the above program, the expression will be as follows

*r = *p/*q;

Then It will show an error message that "Unexpected end of file in comment started on line number --. Compiler is analysing it as, we have written *p and comment symbol /* is started but not closed. To overcome with this error there are two solutions,

Enclose pointer in parenthesis
or
Leave a space after a division operator.

Comments