Pointer Expressions


Like other variables pointer variables can be used in expressions.

1) 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;
*p1=*p1+*p2;
*p1=*p2-*p1;
NOTE: in the third statement there is a blank space between ‘/’ and * because the symbol /*is considered as beginning of the comment and therefore the statement fails.

2) if p1 and p2 are properly declared and initialized pointers then, ‘C’ allows adding integers to a pointer variable.

EX:
int a=5, b=10;
int *p1,*p2;
p1=&a;
p2=&b;

Now,
P1=p1+1=1000+2=1002;
P1=p1+2=1000+ (2*2) =1004;
P1=p1+4=1000+ (2*4) =1008;
P2=p2+2=3000+ (2*2) =3004;
P2=p2+6=3000+ (2*6) =3012;
Here addition means bytes that pointer data type hold are subtracted number of times that is subtracted to the pointer variable.

3) If p1 & p2 are properly declared and initialized, pointers then
‘C’ allows to subtract integers from pointers. From the above example,

P1=p1-1=1000-2=998;
P1=p1-2=1000-4=996;
P1=p1-4=1000-8=992;
P2=p2-2=3000-4=2996;
P2=p2-6=3000-12=2988;

Here the subtraction means byte that pointer data type hold are subtracted number of times that is subtracted to the pointer variable.

4) If p1 & p2 are properly declared and initialize pointers, and both points to the elements of same type. “Subtraction of one pointer from another pointer is also possible".

NOTE: this operation is done when the both pointer variable points to the elements of the same array.
EX:
P2- P1 (It gives the number of elements between p1 and p2)

5) Pointer can also be used with increment and decrement operators.
Ex:
int a=10;
int *b;
b=&a;

EXAMPLE PROGRAM:

Write a function to calculate the roots. The function must use two pointer parameters, one to receive the coefficients a, b and c and the other to send roots to calling function.

#include<stdio.h>
#include<math.h>
Roots (p, q)
    float *p,*q;
    {
    *q= (–(*(p+1) +sqrt ((*(p+1))*(*(p+1))–4*(*p)*(*(p+2))))/
    (2*(*p));
    *(q+1) = (–(*(p+1)-sqrt ((*(p+1))*(*(p+1))–4*(*p)*(*(p+2))))/
    (2*(*p));
    }
    void main ()
    {
    float A [3], R [2];
    int i;
    printf (‘‘Enter values for a, b, c’’);  
    for (i=0; i< = 2; i++)
    scanf (‘‘%f’’, A+i);
    Roots (A, R);
    printf (‘‘root1 = %f’’, *(R+0));
    printf (‘‘root2=%f’’, *(R+1));
}
            
            

Write a ‘C’ program to compute the sum of all elements stored in an array Using pointers.

/*program to compute sum of all elements stored in an
array */
#include<stdio.h>
#include<conio.h>
main ()
{
    int a [10], I, sum=0,*p;
    printf (“enter 10 elements \n”);
    for (i=0; i<10; i++)
    scanf (“%d”, & a[i]);
    p = a;
    for (i = 0; i<10; i++)
    {
    sum = sum*p;
    p++;
    }
    printf (“the sum is % d”, sum);
    getch ();
}


Write a ‘C’ program using pointers to determine the length of a character String.

/*program to find the length of a char string */
#include<stdio.h>
#include<conio.h>
#include<string.h>
main ()
{
    Char str [20].*p;
    Int l=0;
    printf (“enter a string \n”);
    scanf (“ % s”, str);
    p=str;
    while(*p!=’\0’)
    {
    l++;
    p++;
    }
    printf (“the length of the given string is %d”, l);
    getch ();
 }
Copyright © 2018-2020 TutorialToUs. All rights reserved.