Page 15 - Sample_test_Float
P. 15

Functions and Pointers  15



               Example
               void *ptr;
               Consider the below example to clearly understand the void pointer:

               void *ptr;// ptr is declared as Void pointer
               char cnum;
               int inum;
               float fnum;
               ptr = &cnum;// ptr has address of character data
               ptr = &inum; // ptr has address of integer data
               ptr = &fnum;// ptr has address of float data


              Explanation
              We have declared three variables of integer,character and float type.

              •  When we assign address of integer to the void pointer, pointer will become Integer Pointer.
              •  When we assign address of Character Data type to void pointer it will become Character Pointer.
              •  When we assign address of floating point Data type to void pointer it will become floating point Pointer
              •  It is capable of storing address of any data type


              5.2.12. Null Pointer
              •  A pointer which does not point to any memory location is called NULL Pointer.

              Meaning of NULL
              •  NULL is macro constant which has been defined in the header file stdio.h, alloc.h, mem.h, stddef.h and
                 stdlib.h as #define NULL 0


               Example

               int *a;
               int *b = NULL;
               here ‘*a’ and ‘*b’ are not same because b points to NULL and a may point to some garbage address

                                               (a == b)    FALSE, in most cases
                                               (a == NULL )  FALSE, in most case
                                               (b == NULL )  ALWAYS true
               Example program:
               Void main()
               {
                  int *a;
                  int *b =NULL;
                  printf(“value of a =%d \t value of b =%d”,a,b);
               }
               Output:
               Value of a = 28620 value of b = 0


              NULL is a reserved word in most high-level languages. It is an EXPLICIT value, and not an”undefined”value.
   10   11   12   13   14   15   16   17