Pointer variable contain address of same type variable and pointer give value of that address. Every variable in C has a name and a value associated with it. When a variable is declared, a specific block of memory within the computer is allocated to hold the value of that variable. The size of the allocated block depends on the data type. As you know every variable is a memory location and every memory location

has its address defined which can be accessed using ampersand (&) operator which denotes an address in memory.

 

Declaring Pointer Variables

 

The general syntax of declaring pointer variables can be given as below.

                        data_type *ptr_name;

Here, data_type is the data type of the value that the pointer will point to. For example,

int *pnum;

char *pch;

float *pfnum;


Example of Pointer

#include<stdio.h>                                                                          

#include<conio.h>                                                                         

void main(){  
            int i=5,*p;

            clrscr();

            p=&i;                                      

            printf("%u\n",p);                    

            printf("%u\n",&p);                  

            printf("%u\n",*&p);                

            printf("%d\n",*p);                  

            printf("%d\n",**&p);              

            printf("%d\n",*&i);                 

getch();

Output

        65524

        65522

        65524

        5

        5

        5



Null Pointer

So far, we have studied that a pointer variable is a pointer to a variable of some data type. However, in some cases, we may prefer to have a null pointer which is a special pointer value and does not point to any value. This means that a null pointer does not point to any valid memory address.To declare a null pointer, you may use the predefined constant NULL which is defined in several standard header files including <stdio.h>, <stdlib.h>, and <string.h>. After including any of these files in your program, you can write 

int *ptr = NULL;

You can always check whether a given pointer variable stores the address of some variable or contains NULL by writing,

if (ptr == NULL){

Statement block;

}

You may also initialize a pointer as a null pointer by using the constant 0

int *ptr,

ptr = 0;


Pointer to Pointer

In C, you can also use pointers that point to pointers. The pointers in turn point to data or even to other pointers. To declare pointers to pointers, just add an asterisk * for each level of reference. For example, consider the following code:

int x=10;

int *px, **ppx;

px = &x;

ppx = &px;


Exapmle of Pointer to pointer

#include<stdio.h>

#include<conio.h>

void main(){

            int i=5,*p,**q;

            clrscr();

            p=&i;

            q=&p;                                     

            printf("%u\n",q);                    

            printf("%u\n",&q);                  

            printf("%u\n",*&q);                

            printf("%d\n",**q);                

            printf("%d\n",***&q);

            printf("%d\n",*&i);                             

getch();

}

Output

        65522

        65520

        65522

        5

        5

        5


Pointer to Array

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

 

Exapmle of Pointer to Array

#include<stdio.h>

#include<conio.h>

void main(){

            int i,a[5];

            clrscr();

            printf("Enter elements in array :");

            for(i=0;i<5;i++){

                        scanf("%d",&a[i]);

            }

            printf("Elements in array :");

            for(i=0;i<5;i++){

                        printf("%d\t",*(a+i));

            }

getch();

}

Output

        Enter elements in array: 1

        2

        3

        4

        5

        Elements in array:  1   2   3   4   5


Pointer to Structure

Pointer is a variable which points to the address of another variable of any data type like int , char , float etc. Similarly we can have a pointer to structures where a pointer variable can point to the address of a structure variable. For example

struct student stud,*p;

            p=&stud;

            &p->roll

p->name

 

Example of Pointer to structure

#include<stdio.h>

#include<conio.h>

struct student{

            int roll;

            char name[20];

};

void main(){

            struct student stud,*p;

            clrscr();

            p=&stud;

            printf("Enter roll number and name:");

            scanf("%d%s",&p->roll,p->name);

            printf("\nRoll Number:%d\tName:%s",p->roll,p->name);

getch();

}

Output

        Enter roll number and name: 1 Hanuman

        Roll Number: 1 Name: Hanuman