Thursday, December 29, 2011

POINTER

Introduction

Pointer is one of the smartest and strongest part of C. To understand pointer we just have to revise the BASICS part of our blog like

Declaration

Any Variables used in a program has to be declared before using it in a statement and while declaration we can also initialize it.
For example :
                       int a = 5 ;

int is one of that 32 keywords, ' a ' is the variable which is initialized with value 5.

Now int a = 5 ; means that the compiler will allocate space in memory to store the integer number 5 and assign the variable name ' a ' with it as shown below :
There are thousands of memory blocks and each of them has a location number or we can call it an address. That location number always is a whole number. Here 1310588 is the address of the variable ' a '. 
%u is used to print the variable address.
    For example :

    #include "stdio.h"
    main()
    {
            int a=5;

            printf("Value of a = %d  \n", a);

            printf("Address of a = %u  \n\n\n", &a);
    }

    The & "address of" operator is also used before the variable name just like in case of scanf().

    We can also store the address of ' a ' into another variable ' b ', we just have to declare ' b ' as a pointer variable. Syntax for pointer is very simple that is :

    int *b = &a ;

    * is called "value at address"

    int *b ; does not mean that b is declared as integer, it means that b is going to store the address of a variable whose value is an integer number. Because memory location numbers are always a whole number.
    For example :

    #include "stdio.h"
    main()
    {
        int a=5;
        int *b = &a ;

        printf("Address of a = %u  \n\n",&a);
        printf("Address of b = %u  \n\n",&b);

        printf("Value of a = %d  \n\n",a);

        printf("Value at b = %d  \n\n",*b);  /* b is a pointer variable which means
                                                                  b is pointing towards a. */

        printf("Value of b = %u ie address of a  \n\n",b);
        printf("Value of b = %d ie address of a  \n\n",b);
    }

    After executing the program the following output will be shown :

    Note that the variable address will not be same in other computers.

    A pointer variable can also be declared as :

    int **c=&b;

    where **c is a pointer to an integer pointer.
    For example :

    #include"stdio.h"
    void main()
    {
        int a=5;
        int *b=&a;
        int **c=&b;

        printf("Address of a = %u \n\n",&a);
        printf("Address of b = %u \n\n",&b);
        printf("Address of c = %u \n\n",&c);

        printf("--------------------------------------------- \n\n");

        printf("a = %d , *b = %d , **c = %d  \n\n", a, *b, **c);

        printf("&a = %u , b = %u , *c = %u  \n\n", &a, b, *c);

        printf("*c = %u , c = %u , &c = %u  \n\n", *c, c, &c);

        printf("--------------------------------------------- \n\n");
    }

    After executing the program the following output will be shown :


    float and char :

    #include"stdio.h"
    void main()
    {
        float a=5.5;
        char b='x';
        float *az=&a;
        char *bz=&b;

        printf("a = %f  \n",*az);
        printf("b = %c  \n",*bz);
    }

    Addition of Two Numbers :

    #include"stdio.h"
    void main()
    {
        int a=5;
        int b=5;
        int *az=&a;
        int *bz=&b;
        int c = *az + *bz;
        printf("Answer = %d  \n",c);
    }

    Now Getting Back To Functions :

    Call By Value

    #include"stdio.h"
    void swap (int, int);
    main()
    {
        int a,b;

        printf("Enter value for A : ");
        scanf("%d",&a);

        printf("Enter value for B : ");
        scanf("%d",&b);

        swap(a,b);  //  Here only the values of ' a ' and ' b ' are passed

        printf("Now See This ! \n");
        printf("\nA=%d    B=%d  \n\n",*(&a),*(&b));
    }

    void swap (int a, int b)
    {
        int c;
        c=a;
        a=b;
        b=c;

        printf("\nFrom Function ! \n");
        printf("\nA=%d    B=%d  \n\n",a,b);
    }

    Call By Reference

    #include"stdio.h"
    void swap (int *, int *);
    main()
    {
        int a,b;

        printf("Enter value for A : ");
        scanf("%d",&a);

        printf("Enter value for B : ");
        scanf("%d",&b);

        swap(&a,&b);  //  Here the address of ' a ' and ' b ' are passed

        printf("Now See This ! \n");
        printf("\nA=%d    B=%d  \n\n",*(&a),*(&b));
    }

    void swap (int *a, int *b)
    {
        int c;
         c=*a;
        *a=*b;
        *b=c;

        printf("\nFrom Function ! \n");
        printf("\nA=%d    B=%d  \n\n",*a,*b);
    }

    • So with the help of pointer we can directly hit the address of a variable and make changes from another function.
    • A Function returns a single value, with the help of this we can manipulate more than one variables.
    For example :

    #include"stdio.h"
    void cal (int, int, int *, int *);
    main()
    {
        int a, b, sum, sub;

        printf("Enter value for A : ");
        scanf("%d",&a);

        printf("Enter value for B : ");
        scanf("%d",&b);

        cal(a,b,&sum, &sub);

        printf("A + B = %d  \n", sum);
        printf("A - B = %d  \n", sub);
    }

    void cal (int a, int b, int *sum, int *sub)
    {
        *sum=a+b;
        *sub=a-b;
    }



    No comments:

    Post a Comment

    Followers