Wikipedia

Hasil penelusuran

Kamis, 19 November 2015

Pointer & Reference in C

What is a Pointer ? 

A Pointer is a variable whose value is the address of another variable, or direct address of the memory location. Like any variable or constant, you must declare a pointer before using it to store any variable address. 
 
Format :   type *var-name;
  • type is the pointer's base type; it must be a valid C data type 
  • var-name is the name of the pointer variable. 
  • The asterisk (*) used to declare a pointer
     
     Example of a Valid format : 
     
    - int    *ip;    /* pointer to an integer */
    - double *dp;    /* pointer to a double */
    - float  *fp;    /* pointer to a float */
    - char   *ch     /* pointer to a character */ 
     
     
    Example How To Use a Pointer:
     
     
     
     
     
    Relation Between Pointer and Array 
     
    •  An array name is a constant pointer to the first element of the array.
       Example :
       

    •  Once we have the address in p,
             *p will give us the value available at the address stored in p
     
     
     
    Relation Between Pointer and Function  
     
    • C programming allows passing a pointer to a function. 
      We just need to declare the function parameter as a pointer type.
       
      here is an example where we pass a pointer to a function 
      and change the value inside the function which reflects back in the 
      calling function 
     
     
    source : www.tutorialspoint.com
     
    
    
    Reference
    
    
    Theres also one more kind of passing, called Passing By Reference
    Basically, A reference is an alias for another variable
    the syntax/ Format for this Reference is :
    'type &variable' and it only works on c++ compiler
    Heres what i learned For Example :
    
    
    
    
     
     
     
     
     
    
    
    
    
    
    
    
    
    
    
    
    

Tidak ada komentar:

Posting Komentar