pointer


A pointer is a variable which is used to store address of another variable. Whenever a variable declaration is done, it tells the compiler three things.

For eg., consider the following declaration:

int a=5;
It tells to
(i)                 Reserve space in memory to hold the integer value.
(ii)               Associate the name ‘a’ with this memory location
(iii)             Store the value 5 at this location.




The address of a variable can be printed using the operator ‘&’. This is known as ‘address of’ operator.
        
Eg.,
            void main( )
            {
            int a=5;
            printf(“address of %d is %u”, a, &a);
           }
Where the control string %u denotes to print an unsigned integer.

NOTE:-The ‘&’ operator can be used only with a simple variable or an array element. It cannot be used with constants, array names and expression.

Eg.,
            &45,
            &(a+b),
            int a[5]; &a are illegal.

Declaration of pointers:-

            Like normal variables, a pointer variable should also be declared before it is used. It can be done in the following form:
Syntax:-
            datatype *variablename;
Here, ‘*’ is known as ‘value at address’ operator or indirection operator.


Ø  It tells that the variable is a pointer variable.
Ø  Datatype indicates the datatype of the value which is stored in the address location indicated by the pointer variable.

Eg.,
            int *p;
It indicates that p is a variable, which stores the address of an integer value.

            float *q;
It indicates that q is a variable, which stores the address of a float value.

            char *r;
It indicates that r is a variable, which stores the address of a character value.

Initialization of pointers:-

            Assigning address of some variable to a pointer is known as pointer initialization.

Eg.,
            int  x,*pt;
                   pt=&x;
            void main( )
            {
            int *p,a=5;
            p=&a;
            pirntf(“%d is at addres %u”,a,p);
            }

Accessing values of variables:-

            The value of a variable can be accessed in two ways. One way is to refer to the variable through its name and other way is to refer through the address location.

Eg., prog:-
            void main( )
            {
            int a=5,*p;
            float b=2.5,*q;
            char c=’a’,*r;
            p=&a;
            q=&b;
            r=&c;
            printf(“a=%d, address of a=%u”,*p,p);
            printf(“b=%d, address of b=%u”,*q,q);
            printf(“c=%d, address of c=%u”,*r,r);
            }

NOTE: - Whatever may be the datatype at the declaration of pointer variable, it takes 2 bytes of memory since the value of pointer variable is always an unsigned integer.

Ø  A Program to perform arithmetic operations using pointers.
Ø   
#include<stdio.h>
#include<conio.h>
void main( )
{
        int a,b,*p1,*p2;
        p1=&a;
        p2=&b;
printf(“enter any two values”);
scanf(“%d%d”,&a,&b);
printf(“addition=%d”,(*p1+*p2);
printf(“subtraction=%d”,(*p1+*p2);
printf(“product=%d”,(*p1+*p2);
printf(“division=%d”,(*p1+*p2);
getch( );
 }

No comments:

Post a Comment