This blog is under construction

Saturday, 5 October 2013

How pointers work in c?

A pointer is declared by putting an asterisk '*' before the variable name.

int var;     /* variable declaration */
int *ptr;   /* pointer declaration */

Asterisk and ampersand are the frequently used pointer operators.
& - used to get the address of the variable
*  - used to get the value to which the pointer points to

Let us understand the purpose of ampersand and asterisk operators.
var    - variable of type integer
&var  - pointer to an integer variable var
ptr     - pointer to an integer
*ptr   - integer

Consider the following example,
int var = 10;

  name: var
+-------------+
| value: 10  |
+-------------+
  addr: 3000

The above statement/diagram indicates that, var is the name of the variable and its address is 3000. The value at the address 3000 is 10.

int *ptr1, *ptr2;
ptr1 = &var;

  name: ptr1
+---------------+
| value: 3000 |
+---------------+
  addr: 6000

We have assigned the address of the variable var to pointer ptr.

ptr2 = ptr1;

  name: ptr2
+---------------+
| value: 3000 |
+---------------+
  addr: 7000

Now, we have assigned the value of pointer ptr1 to ptr2.  So, both ptr1 and ptr2 refers to the same address(3000).

If we dereference ptr1 or ptr2, we will get the value of the variable var.

Value of ptr1 and ptr2 is 3000.  The value at the memory location 3000 is 10.

  name: var
+-------------+
| value: 10  |
+-------------+
  addr: 3000

So, the value of *ptr1 and *ptr2 is 10.

If we change the value of *ptr1, *ptr2 or var, the same would be reflected in all places.(i.e). the values of *ptr1, *ptr2 and var will be updated with new value.

Let us change the value of *ptr1 to 20.  In this case, the value at the memory location 3000 is updated with new value 20.  So, the value of *ptr1, *ptr2 and var becomes 20.  It's because ptr1, ptr2 and &var points to the same memory location.

Let us describe the above operation using a simple c program.


  #include <stdio.h>
  int main() {
        int var = 10;
        int *ptr1, *ptr2;
        /* assigning addresss of variable var */
        ptr1 = &var;
        /* assinging value of ptr1 to ptr2 */
        ptr2 = ptr1;

        /* printing the addresses and values of variables */
        printf("Address of var is 0x%x\n", &var);
        printf("Address of ptr1 is 0x%x\n", &ptr1);
        printf("Address of ptr2 is 0x%x\n", &ptr2);
        printf("Value of var is %d\n", var);
        printf("Value of ptr1 is 0x%x\n", ptr1);
        printf("Value of ptr2 is 0x%x\n", ptr2);
        printf("Value of *ptr1 is %d\n", *ptr1);
        printf("Value of *ptr2 is %d\n", *ptr2);

        /* updating the value of *ptr2 */
        *ptr2 = 25;
        printf("\n\n###############################\n");
        printf("Changed the value of *ptr2 to %d\n", *ptr2);

        /* printing the addresses and values of variables */
        printf("Address of var is 0x%x\n", &var);
        printf("Address of ptr1 is 0x%x\n", &ptr1);
        printf("Address of ptr2 is 0x%x\n", &ptr2);
        printf("Value of var is %d\n", var);
        printf("Value of ptr1 is 0x%x\n", ptr1);
        printf("Value of ptr2 is 0x%x\n", ptr2);
        printf("Value of *ptr1 is %d\n", *ptr1);
        printf("Value of *ptr2 is %d\n", *ptr2);

        *ptr1 = 30;
        printf("\n\n################################\n");
        printf("Changed the value of *ptr1 to %d\n", *ptr1);
        printf("Address of var is 0x%x\n", &var);
        printf("Address of ptr1 is 0x%x\n", &ptr1);
        printf("Address of ptr2 is 0x%x\n", &ptr2);
        printf("Value of var is %d\n", var);
        printf("Value of ptr1 is 0x%x\n", ptr1);
        printf("Value of ptr2 is 0x%x\n", ptr2);
        printf("Value of *ptr1 is %d\n", *ptr1);
        printf("Value of *ptr2 is %d\n", *ptr2);

        var = 33;
        printf("\n\n################################\n");
        printf("Changed the value of var to %d\n", var);
        printf("Address of var is 0x%x\n", &var);
        printf("Address of ptr1 is 0x%x\n", &ptr1);
        printf("Address of ptr2 is 0x%x\n", &ptr2);
        printf("Value of var is %d\n", var);
        printf("Value of ptr1 is 0x%x\n", ptr1);
        printf("Value of ptr2 is 0x%x\n", ptr2);
        printf("Value of *ptr1 is %d\n", *ptr1);
        printf("Value of *ptr2 is %d\n", *ptr2);
        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Address of var is 0xbffb35bc
  Address of ptr1 is 0xbffb35b8
  Address of ptr2 is 0xbffb35b4
  Value of var is 10
  Value of ptr1 is 0xbffb35bc
  Value of ptr2 is 0xbffb35bc
  Value of *ptr1 is 10
  Value of *ptr2 is 10

  ###############################
  Changed the value of *ptr2 to 25
  Address of var is 0xbffb35bc
  Address of ptr1 is 0xbffb35b8
  Address of ptr2 is 0xbffb35b4
  Value of var is 25
  Value of ptr1 is 0xbffb35bc
  Value of ptr2 is 0xbffb35bc
  Value of *ptr1 is 25
  Value of *ptr2 is 25

  ################################
  Changed the value of *ptr1 to 30
  Address of var is 0xbffb35bc
  Address of ptr1 is 0xbffb35b8
  Address of ptr2 is 0xbffb35b4
  Value of var is 30
  Value of ptr1 is 0xbffb35bc
  Value of ptr2 is 0xbffb35bc
  Value of *ptr1 is 30
  Value of *ptr2 is 30

 ################################
  Changed the value of var to 33
  Address of var is 0xbffb35bc
  Address of ptr1 is 0xbffb35b8
  Address of ptr2 is 0xbffb35b4
  Value of var is 33
  Value of ptr1 is 0xbffb35bc
  Value of ptr2 is 0xbffb35bc
  Value of *ptr1 is 33
  Value of *ptr2 is 33


No comments:

Post a Comment