Skip to main content

5. Pointers

 

Address Operator

It is a unary variable which tells the address of a identifier.

#include <stdio.h>

int main()
{
    int a = 10;
    printf("Address of variable a: %u\n", &a);
    printf("Address of variable a in hexadecimal: %p\n", &a);
    printf("Address of main function: %u\n", &main);
    printf("Address of main function in hexadecimal: %p", &main);
}
  1. What of the following printf() statements will not give error?
  2. printf("%u\n", &a);
        printf("%u\n", &10);
        printf("%u\n", &(a+b));

Pointers

  • Pointers are used to store address of a variable.
  • Size of all the pointer variables is same i.e., 2 or 4 bytes depends on the system.
  • If we simply declare our pointer and do not assign it, then it will be assigned by a garbage value.
  • #include <stdio.h>

    int main()
    {
        int *iptr;
        printf("%u", iptr);
    }
  • If we want our free out pointer variable, then we can assign it NULL or 0. 
  • #include <stdio.h>

    int main()
    {
        int a = 10;
        int *iptr = &a;
        printf("%u\n", iptr);
        iptr = NULL;
        printf("%u\n", iptr);
    }
  1. What is the need of data type while declaring a pointer variable?
  2. Why pointers are removed from Python and Java?
  3. What is the output of the following C code?
  4. #include <stdio.h>

    int main()
    {
        int a = 10;
        float *fptr = &a;
        printf("%d", *fptr);
    }
    1. Compiler error
    2. Wrong output
    3. Correct output

Addresses can be accessed in two ways:

  1. Random Access Memory: Any memory location can be directly accessed if address is known.
  2. Sequential Access: We have to traverse all previous addresses to reach final address.
  1. What is the output of the following C code?
  2. #include <stdio.h>

    int main()
    {
        int a = 87;
        float b = 4.5;
        int *p1 = &a;
        float *p2 = &b;
        printf("Value of p1 = Address of a = %p %p\n", p1, &a);
        printf("Value of p2 = Address of b = %p %p\n", p2, &a);
        printf("Address of p1 = %p\n", &p1);
        printf("Address of p2 = %p\n", &p2);
        printf("Value of a = %d %d %d\n", a, *p1, *(&a));
        printf("Value of b = %.1f %.1f %.1f", b, *p2, *(&b));
    }
  3. What is the output of the following C code?
  4. #include <stdio.h>

    int main()
    {
        int a = 5, *pi = &a;
        float b = 5.5, *pf = &b;
        char c = 'a', *pc = &c;
        printf("Value of pi = Address of a = %p\n", pi);
        printf("Value of pi = Address of b = %p\n", pf);
        printf("Value of pi = Address of c = %p\n", pc);
        pi++;
        pf++;
        pc++;
        printf("Now value of pi = %p\n", pi);
        printf("Now value of pf = %p\n", pf);
        printf("Now value of pc = %p\n", pc);
    }
  5. What is the output of the following C code?
  6. #include <stdio.h>

    int main()
    {
        int a, b;
        a = 15, b = 20;
        int *p = &a;
        a = *p++;
        printf("%d %d\n", a, b);
        *p = &a;
        printf("%d\n", (*p)++);
        printf("%d\n", *++p);
        printf("%d", ++*p);
    }

Pointer Arithmetic

The following operations are valid with pointers:

  • Addition with integer constant i.g., (iptr + 4)
  • Subtraction with integer constant i.g., (iptr - 3)
  • Post-increment operation e.g., iptr++
  • Pre-increment operation e.g., ++iptr
  • Post-decrement operation e.g., iptr--
  • Pre-decrement operation e.g., --iptr
  • Subtraction of two pointer variables e.g., ptr1-ptr2;

Addition, multiplication and division of two pointer variables is not a valid operation.

Pointer Comparions

We can also compare two pointers, if both points to the same address or NULL value, then they are equal.

#include <stdio.h>

int main()
{
    int a = 10, b = 20;
    int *iptr1, *iptr2;
    float x = 12.0;
    float *fptr = &x;
    printf("%d\n", iptr1 == iptr2);
    iptr1 = NULL;
    iptr2 = NULL;
    printf("%d\n", iptr1 == iptr2);
    iptr1 = &a;
    iptr2 = &b;
    printf("%d\n", iptr1 == iptr2);
    printf("%d\n", iptr1 >= fptr);
}

Pointers to Pointers

We can also store the address of a pointer variable with the help of double pointer or pointer to pointer variable.

#include <stdio.h>

int main()
{
    int a = 10;
    int *p = &a;
    int **q = &p;
    printf("Address of a = %d = Value of p: %d\n", &a, iptr1);
    printf("Address of p = %d  Value of q: %d", &iptr1, iptr2);
}

Similarly, we can also define three pointers or more.


Prev Next

Comments