Thursday, February 16, 2012

Pointer in C - A Brief Explanation.



A Pointer in C is a special kind of variable which can store address of another variable. ‘C’ is not an object oriented programming so creating objects are not allowed but use of pointer and function pointer allow C to be used in a more object orientated way; C doesn't support objects, but a structure can contain a collection of data and function pointers so the structure can effectively contain data and the functions needed to process it. A Pointer declare with ‘*’ and the unary operator `&' is used to produce the address of an object.

Reference operator (&)
When we declare a variable the amount of memory needed is assigned for it at a specific location in memory. To know the exact location of the variable we use & operator.

Dereference operator (*)
A variable which stores a reference to another variable is called a pointer. Pointer variable should be declare using deference operator.

Pointer Arithmetic’s
We can perform addition and subtraction operations with pointers. By addition and subtraction pointers move according to the size of the data type size occupied in the memory for which the said pointer is pointing.

int x, *p;
p = &x;   //Address of integer variable a stored to a Pointer *p

String with pointer.

       char *ch;
       char ch1[50];  
       ch = &ch1[0];    //Address of ‘0’ location stored to a Pointer character *ch.

Pointer and Array
int a[10],*p;
p=&a[0];

Function Pointer

Function Pointers are pointers type variables, which point to the address of a function.  The executable compiled program code and the used variables, are put inside this memory.

Example

#include <stdio.h>
#include <conio.h>

   void main()
      {
           int a,*a1,b;
           clrscr();
           a=20;
           b=a;
           a1=&a;
           printf("  a = %d   &a = %u   a1  = %u a1 = %d  b = %d \n",a,&a,a1,*a1,b);
           a=a+5;
           printf("  a = %d   &a = %u   a1  = %u  a1 = %d  b = %d ",a,&a,a1,*a1,b);
           getch();
      }
Here address of variable ‘a’ is assigned t pointer ‘p’ and value stored to a variable ‘b’.

output  –

  a = 20   &a = 65524   a1  = 65524 a1 = 20  b = 20
  a = 25   &a = 65524   a1  = 65524  a1 = 25  b = 20

Output are the values of variable ‘a’ and the address of ‘a’ but in second case value of a change and variable ‘b’ is unchaged  but the pointer ‘a1’ that pointing to the variable ‘a’ is changes.

Pointers pointing to another Pointer

#include <stdio.h>
#include <conio.h>

      void main()
           {
              int a,*i,**j,***k;
              clrscr();
              a=10;
              i=&a;
              j=&i;
              k=&j;
              printf(" a = %d  i = %u  j = %u    k = %u ",a,i,j,k);
              printf(" a = %d  i = %d  j = %d    k = %d ",a,*i,**j,***k);
              getch();
           }
Output -
       a = 10  i = 65524  j = 65522    k = 65520
       a = 10  i = 10  j = 10    k = 10

Pointer and String. Printing each character and counting the string length.

#include <stdio.h>
#include <conio.h>
  void main()
      {
           char *s="bay of bengal";
           int n;
           clrscr();
           for(n=0; *s != '\0'; s++ )
              {
                   printf("   %c\n ",*s);
                   n++;
              }
           printf(" Nos of character  %d",n);
           getch();
      }

Output -

   b
   a
   y

   o
   f

   b
   e
   n
   g
   a
   l
 Nos of character  13


Pointer and Array

#include <stdio.h>
#include <conio.h>

  void main()
      {
           int arr[]={5,3,9,12}, *ptr;
           clrscr();
           ptr=&arr[0];
           printf("\n\tOutput – ")
           while(*ptr)
              {
                   printf("%d ",*ptr);
                   ptr++;
              }
           getch();
      }


         Output -  5 3 9 12


No comments: