Pages

Pointer


Pointer       Pointer is  a special  type of  variable  which is used  to store the  address of another variable .This variable  starts  with asterisk  symbol ( * ).The  pointer  can use  two  special  operators..

1        The first one is  * asterisk  .it is called  indirection operator or value of operator
2        The  second is &  ampersand  it is called  address of operator  .it is used  to  get  address .


Pointer  declaration :   A  pointer  is declared same  as a variable  with a specific  data type .But declared  with and asterisk  sign . it show given below


          Data type   * pointer name ;
           Int  *p;
                                     

Address operator &(ampersand)

It is used to assign the address of the variable  to the pointer variable  .For eg

Int  a, *p;
a = 5;
p =  & a;                             // declare the address  of the variable


Indirection operator  *  ( asterisk   )

This operator is also  know  as value ot operator ,which is used to print the value ot the variable through pointer variable . The indirection operator  is used to declare a pointer  For  eg .

int  a , *p;

a = 5;
p = & a;



#include <stdio.h>
#include <conio.h>
void  main( )
{
Int a ,*b;
a = 10;
b = &a;
printf ( “\n the value of a =%d”, a  );
printf (“\n the address of a =%u”, &a);
getch ( );
}     


Advantage of Pointer :

1 Pointer increase the speed of the  program
2 it is used to read  the address of the variable
3 it can pass as argument into the function
4 Pointer can be used to access elements ot the structure
5 it is used to the manage memory efficiently


Dynamic memory allocation :  it means memory which is allocated at run time in place of compile time .in normal array or variable the memory is allocated at compile time .The memory which is allocated at run time is know as dynamic memory allocation .This memory is accessed  using pointer .These  function are given below :

1 malloc ( )
2 calloc ( )
3 realloc ( )
4 free ( )

1 malloc ( )  this ( ) means  to allocate  the memory .This function is used to allocate dynamic memory a run time to the pointer variable .

2 calloc ( )  this ( ) is also used for dynamic memory allocation same as malloc  ( ) but this ( ) contain two argument .The first  argument tells the number of elements and the second argument tells their size

Syntax  void  calloc ( number of item ,size );

3 realloc ( )  it means reallocation of memory .This ( ) is used  to reallocate memory of the pre- allocated pointer variable . it is used to increase and decrease the size of the memory .

4  free ( )   it is used to release the memory which is occupied by the variable .  it free the memory . This function contain a single argument whose memory we want to releae .

Syntax  void free ( pointer variable );

                    Free ( *a ); 

0 comments: