Sunday 16 August 2015

C language Pointers Tutorials


Pointers

   Pointer is a variable which stores address of another variable.

--> The syntax for declaring a pointer is as follows.

datatype  *<var_name>

   Eg:-   int  *a;

--> Function calls are of two types

1) call-by-value:

   Here the values of actual parameters get copied to formal parameters.

2) call-by-reference:

   Here the address of actual parameters get copied to formal parameters.

--> Any pointer variable required 2 bytes of memory irrespective of their datatype.

--> A pointer variable of some datatype can store address of a variable of same datatype i.e int * can store address of a variable, float * can store address of a float variable etc.

--> A pointer which is capable of storing address of a variable of any datatype is void *.

--> The address stored in a pointer in always being an unsigned integer.

--> When we have a pointer containing address of a variable, then we can access the value contained is that variable into the pointer using an operator called De-referencing operator, represented with the symbol *.

Program:

#include< stdio.h >
#include< conio.h >
void main( )
{

      int x, *a;
      clrscr( );
      printf("\nEnter X Value: ");
      scanf("%d", &x);
      a = &x;
      printf("\n\tAddress: %u", a);
      printf("\n\tValue : %d", x);
      x = x + 7;
      printf("\n\tValue : %d", *a);
}

Advantages:

--> Instead of maintaining multiple copies of data, we can maintain a single copy and send its address to all the other peoples.

--> When large volumes of data is to be copied from one function to the other function instead of sending the entire data we can just send only the address, there by saving lot of time.

--> We can also allocate memory as much as we requires as and when required.

Dynamic memory allocation:

Allocating memory as much as we require as and when required is refered as dynamic memory allocation.

--> C language provide dynamic memory allocation using the following functions.

1) malloc( )

2) calloc( )

3) realloc( )

4) free( )

--> These functions are available in the header file alloc.h

1) malloc( ):

   Syntax:

            void * malloc(no. of bytes);

--> This function expect number of bytes for which the memory is to be allocated as argument.

--> It allocates the specified amount of memory and return its address.

--> It doesn't talk about what type of values to store in the allocated memory. So, its return type is void *

--> So, we need to type casting to the required pointer type when a pointer contain address of an array, then the pointer itself behavior like an array.

Program:

#include< stdio.h >
#include< conio.h >
#include< alloc.h >
void readarray(int *, int);
void printarray(int *, int);
void main( )
{

      int *a, n;
      clrscr( );
      printf("\nEnter N Value: ");
      scanf("%d",&n);
      a = (int *)malloc(n*sizeof(int));
      readarray(a, n);
      printarray(a, n);

}
void readarray(int *x, int n)
{

      int i;
      printf("\nEnter array elements: ");
      for(i=0;i<n;i++)
      scanf("%d",&x[i]);
}
void printarray(int *x, int n)
{

      int i;
      printf("\nThe elements in the array are:");
      for(i=0;i<n;i++)
      printf("%d ", x[i]);

}

2) calloc( ):

It is same as malloc( ) except that the values are initiated to zero of allocating the memory.

--> It is used to allocate multiple block of memory.

   Syntax:

            void * calloc(no. of elements, elements size);

3) realloc( ):

It is useful when we are required to adjust already allocated memory.

   Syntax:

            void * realloc(void *, size)

4) free( )

It is used to clear the allocated memory.

   Syntax:

            void  free(void *)




0 comments:

Post a Comment