Friday, 29 June 2018

Pointers in C


  • what is Pointer in C !
pointer are the special type of variables which is used to store the address the address of another variable. As we know that the ordinary variable can contain only a value of its types it is not possible to assign an address to an ordinary variable.

How to declare a pointer variables .

Syntax :  data type * variable name ;                     *   means = in directional operator (asterisk symbol)

Examples :
                  int * a ;                                 /* pointer to integers */
                 char * a ;                              /* pointer to character */
                 float * a ;                             /* pointer to float */
The declaration tells the C compiler to :
1. Reserve space in memory to hold particular value .
2. Associate the name i with the memory location .
3. store the value the location

How to assign a pointer variables :

Syntax :  pointer variable name = address of variable ;
                                      OR
               pointer variable name = & name of another variable ;         & = reference operator 

Examples :  
                    a = &b ;        ( pointer assignment )

Program of use of pointer :

# include <stdio.h>
void main () 
{
int a = 10 ;               /* var. declaration and initialization */  
int *b ;                    /* pointer var. declaration */ 
b = &a ;                 /* pointer assignment */
clrscr () ;
printf ("\n %d". a) ;                    value of a = 10
printf ("\n %d", &a) ;                 address of a = FF01
printf ("\n %d", b) ;                   address of a = &a = FF01
printf ("\n %d", &b) ;                address of b = FF05
printf ("\n %d", *b) ;                 value of b becomes = 10
getch () ;
}

How to call a function in C  :  

1. Call by value        /* argument passed by value */                                                          
2. call by reference   /* argument passed by references*/ 

                          *    In call by value method the value of variable is passed in a function call statement and i.e stored on another address in function definition . In this way there will two types of argument .
                      1. Argument in main function  ( Actual argument )
                      2. Argument in function definition ( Formal argument )

                       *     In call by reference method address of argument are passed from main function to the function definition and these address are collected in the pointer variable thus by using call by reference method if we update the value of variable in the function definition than it will be reflected throughout the program .It shows by call by reference method we can return more than one value at a time , which is not possible with ordinary  .

                        
  NOTE :  Uninitialized pointer variable are known as the " Dangling pointer".


                             


No comments:

Post a Comment

Featured post

Program to find Volume of given box in java

class volume { public static void main (String args []) { int l=5,b=6,h=10; int volume; volume=l*b*h; System.out.println (" vol...