Sunday, February 9, 2020

C Language : Pointer and Pointer to Pointer

POINTER:

Memory Addresses:

- Every variable stored in the memory and occupied some spaces and has a unique memory address.
- Memory Address of any variable is an UNSIGNED INTEGER.
- We can access (print) their memory addresses using:

Memory Address Operator
&
Print Specifier
%u

- We can print Memory Size of the variable using sizeof( ) operator which return an INTEGER value as a no. of Bytes.
- sizeof( ) --> int --> %d



// Printing Memory Addresses of the variables.                      MEMO.C
# include<stdio.h>
# include<conio.h>
void main( )
{
        char CH=’Y’;
        int N=99;
        float PER=79.28;
        clrscr( );

        printf(“\n Value of CH = %c”, CH);
        printf(“\n Base Memory Address of CH = %u”, &CH);
        printf(“\n No. of Bytes occupied by CH = %d”, sizeof(CH));

        printf(“\n Value of N = %d”, N);       
        printf(“\n Base Memory Address of N = %u”, &N);
        printf(“\n No. of Bytes occupied by N = %d”, sizeof(N));

        printf(“\n Value of PER = %f”, PER);
        printf(“\n Base Memory Address of PER = %u”, &PER);
        printf(“\n No. of Bytes occupied by PER = %d”, sizeof(PER));
}

~~~~~~@~~~~~~



POINTER:
- Pointer is a special feature of C Language.
- Pointer is a Derived Datatype in C language.
- This is a special type of variable which points to another variable, means it can store Memory Address (reference) of another variable.
- We can access value of that variable by the Pointer.
- A Pointer operator ( * ) is used to declare a POINTER.
- A Memory Address Operator ( & ) is used to assign memory address of the variable to the pointer.
- A pointer may be of any datatype like int, char or float.

Declaration:
     Syntax:
                        datatype *Pointer-Variable;
     Ex:
                        int *P;
                        float *PTR;
                        char *CP;

Assigning Memory Address to the Pointer:
     Syntax:
                        Pointer-Variable=&Another Simple Variable;
     Ex:
                        int N=99, *PTR;
                        PTR=&N;         --> ‘PTR’ points ‘N’


 

// An example program of Pointer.                                          PTR1.C
# include<stdio.h>
# include<conio.h>
void main( )
{
        int N=99, *P;
        clrscr( );

        printf(“\n Value of N = %d”, N);       
        printf(“\n Base Memory Address of N = %u”, &N);

        P=&N;
        printf(“\n Base Memory Address of N = %u”, P);
        printf(“\n Value of N = %d”, *P);

        printf(“\n Base Memory Address of P = %u”, &P);
}

~~~~~~@~~~~~~




POINTER to POINTER:
- This is a Pointer variable which points to another Pointer variable, means it can store Memory Address (reference) of another Pointer variable.
- A Double Pointer operator ( ** ) is used to declare a POINTER to POINTER.
- We can access value of that variable by the P2P which is pointed by Pointer.

Declaration:
     Syntax:
                        datatype **Pointer-to-Pointer;
     Ex:
                        int **PP;

Assigning Memory Address to the P2P:
     Syntax:
                        P2P-Variable=&Another Pointer Variable;
     Ex:
                        int N=99, *PTR, **PP;
                        PTR=&N;         --> ‘PTR’ points to ‘N’
                        PP=&PTR;       --> ‘PP’ points to ‘PTR’




// An example program of Pointer to Pointer.          PTR2.C
# include<stdio.h>
# include<conio.h>
void main( )
{
        int N=99, *PTR, **PP;
        clrscr( );

        printf(“\n Value of N = %d”, N);       
        printf(“\n Base Memory Address of N = %u”, &N);

//--------------

        PTR=&N;
        printf(“\n Base Memory Address of N = %u”, PTR);
        printf(“\n Value of N = %d”, *PTR);

        printf(“\n Base Memory Address of PTR = %u”, &PTR);

//------------

        PP=&PTR;
        printf(“\n Base Memory Address of PTR = %u”, PP);
        printf(“\n Value of N = %d”, **PP);
}

~~~~~~@~~~~~~


Uses of Pointers:
- It can return more than one value from the function.
- To access array elements.
- Pointers are more efficient to manage Arrays and Data tables.
- Pointer reduces length & complexity of the program.
- It increases execution speed of the program and decreases execution time of the program.
- The use of Pointer to String enables saving of data storage space in Memory.
- It is used to implement complex data structures like Linked-List & Binary Trees.
- For low level (system level) programming.
- To find dynamic memory address.
- To pass arrays and strings to the function.
- To access memory address of the variable.



Difference between Array & Pointer:

Array
Pointer
Array is a collection of similar type of data which are stored in contiguous memory allocations.
Pointer is used to store memory address of another variable.
Array elements are accessed using an index variable.
Pointers are used directly, do not required index or another variable.
Array can be initialized during its declaration:
int N[ ] = {5, 6, 7};
Pointers can not be initialized during its declaration:
int *P;
Accessing process of array elements using an INDEX variable is SLOWER.
Accessing process of array elements using a POINTER is FASTER.
Arrays are Static in C.
Pointers are Dynamic in C.



Pointer related programs:

// Sum of two numbers using Pointers.                                   P_SUM.C
# include<stdio.h>
# include<conio.h>
void main( )
{
        int a, b, s, *P, *Q;
        clrscr( );
        printf(“Input First Number = “);
        scanf(“%d”, &a);
        printf(“Input Second Number = “);
        scanf(“%d”, &b);
        P=&a;
        Q=&b;
        s=(*P) + (*Q) ;
        printf(“Sum = %d”, s);
}

~~~~~~@~~~~~~



// Find Simple Interest using Pointers.                                            P_SI.C
# include<stdio.h>
# include<conio.h>
void main( )
{
        int p, t, *P, *T;
        float r, *R, si;
        clrscr( );
        printf(“Input Principal Amount = “);
        scanf(“%d”, &p);
        printf(“Input Rate = “);
        scanf(“%f”, &r);
        printf(“Input Time = “);
        scanf(“%d”, &t);

        P=&p;
        R=&r;
        T=&t;

        si = (*P) * (*R) * (*T) /100;
        printf(“Simple Interest = %f”, si);
}

~~~~~~@~~~~~~



// Find factorial of given number using Pointers.                     P_FACTO.C
# include<stdio.h>
# include<conio.h>
void main( )
{
        int n, i, *P;
        long F=1;
        clrscr( );
        printf(“Input a Number = “);
        scanf(“%d”, &n);

        P=&n;
        for(i=*P; i>=1; i--)
        {
                F = F * i ;
        }
        printf(“Factorial Value = %ld”, F);
}

~~~~~~@~~~~~~


No comments:

Post a Comment