Pointers

Pointers :-

                   A pointer is a special type of variable which is used to store the memory address of basic datatype, of array , structure, union etc .

           Till now , we only have used simple undirection variables in which we can store/retrieve values . But now we will discuss a special type of variables which are used to store the memory location of a variable. Its hard to understand and manipulate values on the basis of there address(with pointers) , but this is the most powerful concept of C programming which

  • Provide Flexibility to the program
  • As we deal with memory addresses , therefore increase execution speed
  • It allows a function to reflect back more from one value at a time
  • It allow us to pass array , structureetc as arguments to functions

Before starting the concept of pointers , I want to introduce you with two operators

  1. 1)Address operator ‘&’     :- This is use to get the address of a variable .
  2. 2)Indirection operator ‘*’   :- It is use to manipulate the value stored in variable which is being pointed from the pointer variable .

 Now Let’s start the concept of pointer variables :-

A pointer variable is a special type of variable that contains the address of another variable of same data type 
Syntax :-

            data_type   *ptr ;

here

data_type         the data type of variable which this pointer will point

ptr                    name of pointer variable

*                      Indirection operator

Now I tell you how a variable is stored in memory .

Example :-

            int   x = 5 ;

            int   *y ;

            y = &x ;

see following diagram.

Here

x is simple variable of integer data type

y is pointer variable which is used to store the address of an integer variable x

2002 is the memory address of variable x (Assumed values)

2004 is the memory address of pointer variable y (Assumed values)

Now consider the following statements .

 One more important thing , the variable , and pointer variable which we want to use to store the address of that variable must be of same data type , because it can arise syntax and logical errors . I discuss it later .

 Now you can understand that how pointers are used to store the address of a variable, But now I tell you how a pointer can store the address of another pointer i.e.


Poniter to Pointer :-

                                    As we know every variable declared in a program is stored at some memory location and has a uniqueaddress , same as simple variables pointers also have there address in memory . Therefore a pointer to pointer variable can be defined which can store the address of a pointer variable .

 Syntax:-

          data_type   **y ;

here we have used **to declare a pointer to pointer variable

I show you with the help of an example.

            int x ;

            int *y ;

            int **k ;

            y = &x ;

            k = &y ;

 Now see the following figure .

 Here

 x is simple variable

y is pointer variable , which is used to store the address of a variable

k is a pointer to pointer variable , which is used to store the address of a pointer variable

2002 , 2004 , 2006 are memory addresses of x , y , k .

Now consider the following statements .

Now lets move to next topic , which is Pointer Arithmetic .


Pointer Arithmetic :-

                                 We all are much familiar with the word arithmetic . We can also perform some arithmetic operations on pointers , like Incrementing , Decrementing , Add/suntract some values . But as we know we are dealing with pointers and memory addresses these are manipulated in different manner . for example :-

            int x ;

            int *p1 ;

            x=5 ;

            p1 = &x ;

 Here we have an integer variable with name ‘x’ and a pointer ‘p1’ pointing towards ‘x’

 Now if we perform Increment operation on x

 x++ ;

 its result is x = 6

 and now if we perform increment operation on pointer p1

 (Let memory address of x = 2002   i.e. p1 = 2002)

 p1++

 the result comes p1 = 2004

 Now the question arises   HOW ?

 I tell you , we know that ‘x’ is an integer variable which takes 2 Bytes in memory and p1 is pointer which contains the address of ‘x’ . When we increment the pointer it goes to the next variable position in memory which is at a difference of 2 Bytes from ‘x’

 See following Figure .

Thus if we use pointer for long and float so it will increment the address from 4 as float and double data type takes 4 Bytes in memory .

Following are the valid operations on Pointers .

 1)Increment and Decrement operation :-

 We can perform pre-Increment (++p) , post-Increment (p++) , Pre-Decrement (- - p) , Post-Decrement (p- - ) operations on Pointers .

 2)Addition and Subtraction of a number from pointer :-

    We can also Add , subtract a number from a pointer variable like p = p + 1 but it work differently .

 Let   *p is an integer pointer variable and contain address 2002(Assumed value) , Now we perform operation .

  

               Statements

               Output

         P = 2002

p = p + 1 ;

         P = 2004

         P = 2002

p = p + 4 ;

         P = 2010

         P = 2002

p = p – 1 ;

         P = 2000

         P = 2002

p = p – 4 ;

         P = 1994

 (I don’t think that I need to explain it again as above )

 There are some more operations valid on pointers , these are :-

1)p1 > p2

  1. 2)p1 < p2
  2. 3)p1 == p2
  3. 4)p1 != p2
  4. 5)p1 >= p2
  5. 6)p1 <= p2

These operations are valid if p1 and p2 are of same data type .


Pointers and Array :-

                                   Pointers and array are very closely related with each other . The name of the array is infact a pointer which points to the first element of the array , i.e. the Base address or starting address of the array .

 
 

Who's online

We have 5 guests and one member online

About Us

Harsimran Singh

Hello

This website is a special gift for Programming Lovers , which want to learn basic Programming Languages and want to develop there skills Read More....

Our work

Find Us

This email address is being protected from spambots. You need JavaScript enabled to view it.

Harsimran Singh(Facebook)

Harsimran Singh(about.me)

Harsimran Singh(G+)