This blog is under construction

Saturday, 5 October 2013

What is a pointer and pointee?

What is a pointer?
A pointer is a variable whose value is the address of another variable.

How to declare a pointer?
Any declaration with an asterisk symbol in front of the variable name creates a pointer.

Consider the following declaration,
int *ptr;
The above declaration creates a pointer named ptr.

Variable Declaration:
Whenever we declare any variable and assign value to it, the value of the variable is stored in a particular memory location.  And the memory location of the variable can be identified using its address.

Consider the following declaration,
int var = 10;

         name: var
     +---------------------+
     |   value:10           |
     +---------------------+
         addr:0xbfaba94c

'var' is the name of the memory space that holds the value 10
0xbfaba94c is the address(memory location) of the integer variable var
10 is the value at the memory location 0xbfaba94c


Pointer Declaration:
Pointer is nothing but the name for an address.  It stores the address(reference) of another variable.

Consider the following example,
int *ptr;
ptr = &var;  /* &var - Address of var */

         name: ptr                                        name: ptr
     +-----------------------+                    +---------------------+
     |  value:0xbfaba94c  |        <=>       |    value: &var     |
     +-----------------------+                    +---------------------+
         addr:0xbfaba948                           addr:0xbfaba948

ptr is the name of the memory location which holds the address of another variable var
0xbfaba948 is the address of the pointer variable
0xbfaba94c is the value at the address 0xbfaba948.  And it is the reference to another variable var.

Let us print the addresses and values of the variables using the below program.


  #include <stdio.h>
  int main() {
        int var = 10;
        int *ptr;

        /* assigning address of the variable var to pointer ptr */
        ptr = &var;

        /* printing address of the variable var */
        printf("Address of var: 0x%x\n", &var);
        /* printing value of the variable var */
        printf("Value of var  : %d\n", var);

        /* printing address of the variable ptr */
        printf("Address of ptr: 0x%x\n", &ptr);
        /* printing value of the pointer ptr  - address of variable var */
        printf("Value of ptr  : 0x%x\n", ptr);
        return 0;
  }




  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Address of var: 0xbfaba94c
  Value of var  : 10
  Address of ptr: 0xbfaba948
  Value of ptr  : 0xbfaba94c



What is pointee?
The data referenced by a pointer is called pointee.
int *ptr, var = 10;
ptr = &var;

Here, pointer ptr refers to the address of the variable var.  So, var is pointee.

4 comments:

  1. Very good presentation of Pointer

    ReplyDelete
  2. Dell Laptop Repair Center in Noida is no.1 service center which provides door to door services in or its nearby areas. We have expert, technicians who can repair your laptop at your home. . Call us: 9891868324

    ReplyDelete