C tutorial- Pointer in C programming language

Programming languages or concepts

Pointer in C programming language

In this tutorial, if to full use of  c programming language , you have to must good understanding of pointer in c. You have to learn pointers because they are used everywhere in the C language. Once you understand full concept of pointer you can easily used in your C programming. 

Following is a some topics that's we cover in this;-

   What is a pointer in C

    Declaration of pointer

   How to use pointers

   Null pointer in C

   Array to pointer


1).  What is a pointer in C

   a pointer is a special types of variable that store the address of another variables.

  a pointer is a similar to a variable but the different is that point store the address of the location in the memory and variable stored the value.

  a pointer is a variable whose value is the address of another variable, means a direct address of the memory location.

  you must declare a pointer before you can use it to store any variable address.

Ex.

int *ptr;


2). Declaration of pointer

  Digital form of pointer variable declaration is

       Data_Type *variable_name;

    Here, type is the pointers best type, it must be a valid C data type and variable_name is the name of the pointer variable. The asterisk '*' you use to declare a pointer is the same as asterisk that you can use the for multiplication.


Some value declaration examples:-

    int *ip;          /*pointer to an integer*/

   double *dp;   /*pointer to an double */

   float *cp;        /* pointer to an float*/


3) how to use pointers?

This is is done by using unary operators * that returns the value of the variable located at the address specified by the operant.

   Ex.

    #include <stdio.h>

int main ()

{

int var = 50; /* actual variable declaration */


int *ip; /* pointer variable declaration */


ip = &var; /* store address of var in pointer variable*/


printf("Address of var variable: %x\n", &var );

/* address stored in pointer variable */


printf("Addr stored in ip variable: %x\n", ip );

/* access the value using the pointer */


printf("Value of *ip variable: %d\n", *ip );


return 0;

}


4) null pointer in C 

A pointer that is assign null is called a null pointer.

It is always a good practice to assign a null value to a pointer variable in case you don't have any exact address to be assigned.

 the null pointer is a constant with a value of zero defined in server standard libraries.

Following program for null pointer


#include <stdio.h>

int main ()

{

int *ptr = NULL;

printf("The value of ptr is : %x\n", ptr );

return 0;

}


Output:

    The value of ptr is : 0



Array to pointer

we can use a point to point to an array and then we can use that pointer to access the array elements.

#include<studio.h>
int main ( )
{
   int I ;
   int a[10] = { 2,3,5,6,7,2,9,5,0,1};
   int *p = a;
   for (i=0; i<10;i++)
    {
         Printf("%d",*p);
           p++;

     }
  return 0;
 }




close