What is pointer?

A pointer is a variable. that points to an address of a value.
When you define a variable in your program, the compiler allocates a memory location with a unique address to store that variable’s value. then you access the memory address through the pointer variable name.

For example, when you define a variable:

int x = 10;
You specified variable name ( x ), its data type int and its value, 10. The variable x resides in the memory with a unique memory address. To get the memory address of the variable x, you use the unary operator & as follows:
printf("The memory address of x is %p\n",&x);


Address in C :-

Whenever a variable is defined in C language, a memory location is assigned for it, in which it's value will be stored. We can easily check this memory address, using the & symbol.
If var is the name of the variable, then &var will give it's address.

#include<stdio.h>
void main()
{
int var = 7;
printf("Value of the variable var is: %d\n", var);
printf("Memory address of the variable var is: %x\n", &var);
}

Output :-

Value of the variable var is: 7
Memory address of the variable var is: bcc7a00

Concept of Pointers :-

Whenever a variable is declared in a program, system allocates a location i.e an address to that variable in the memory, to hold the assigned value. This location has its own address number.
Let us assume that system has allocated memory location 80F for a variable a.
int a = 10;


The variables which are used to hold memory addresses are called Pointer variables.
A pointer variable is therefore nothing but a variable which holds an address of some other variable. And the value of a pointer variable gets stored in another memory location.



Benefits of using pointers :-

  • Pointers are more efficient in handling Arrays and Structures.
  • Pointers allow references to function and thereby helps in passing of function as arguments to other functions.
  • It reduces length of the program and its execution time as well.
  • It allows C language to support Dynamic Memory management.
  • We can return multiple values from function using pointer.

  • Declaring a pointer :-

    datatype *pointer_name;
    Data type of a pointer must be same as the data type of the variable to which the pointer variable is pointing. void type pointer works with all data types, but is not often used.


    int *ip // pointer to integer variable
    float *fp; // pointer to float variable
    double *dp; // pointer to double variable
    char *cp; // pointer to char variable

    Initialization of Pointer variable :-

    Pointer Initialization is the process of assigning address of a variable to a pointer variable. Pointer variable can only contain address of a variable of the same data type. In C language address operator & is used to determine the address of a variable. The & returns the address of the variable associated with it.

    #include<stdio.h>
    void main()
    {
    int a = 10;
    int *ptr; //pointer declaration
    ptr = &a; //pointer initialization
    }

    #include<stdio.h>
    void main()
    {
    float a;
    int *ptr;
    ptr = &a; // ERROR, type mismatch
    }

    Output :-

    If you are not sure about which variable's address to assign to a pointer variable while declaration, it is recommended to assign a NULL value to your pointer variable. A pointer which is assigned a NULL value is called a NULL pointer.
    #include <stdio.h>
    int main()
    {
    int *ptr = NULL;
    return 0;
    }

    #include <stdio.h>
    int main()
    {
    int a, *p; // declaring the variable and pointer
    a = 10;
    p = &a; // initializing the pointer
    printf("%d", *p); //this will print the value of 'a'
    printf("%d", *&a); //this will also print the value of 'a'
    printf("%u", &a); //this will print the address of 'a'
    printf("%u", p); //this will also print the address of 'a'
    printf("%u", &p); //this will print the address of 'p'
    return 0;
    }

    Points to remember while using pointers: :-

  • While declaring/initializing the pointer variable, * indicates that the variable is a pointer.
  • The address of any variable is given by preceding the variable name with Ampersand &.
  • The pointer variable stores the address of a variable. The declaration int *a doesn't mean that a is going to contain an integer value. It means that a is going to contain the address of a variable storing integer value.
  • To access the value of a certain address stored by a pointer variable, * is used. Here, the * can be read as 'value at'.

  • #include <stdio.h>
    int main(){
    int number=50;
    int *p;
    p=&number;//stores the address of number variable
    printf("Address of p variable is %x \n",p);
    printf("Value of p variable is %d \n",*p);
    return 0;
    }

    output
    Address of number variable is fff4
    Address of p variable is fff4
    Value of p variable is 50


    NULL Pointer :-

    A pointer that is not assigned any value but NULL is known as NULL pointer. If you don't have any address to be specified in the pointer at the time of declaration, you can assign NULL value.
    int *p=NULL;

    #include <stdio.h>
    int main(){
    int a=10,b=20,*p1=&a,*p2=&b;
    printf("Before swap: *p1=%d *p2=%d",*p1,*p2);
    *p1=*p1+*p2;
    *p2=*p1-*p2;
    *p1=*p1-*p2;
    printf("\nAfter swap: *p1=%d *p2=%d",*p1,*p2);
    return 0;
    }

    output :-

    Before swap: *p1=10 *p2=20
    After swap: *p1=20 *p2=10

    Pointer to a Pointer(Double Pointer) :-

    Pointers are used to store the address of other variables of similar datatype. But if you want to store the address of a pointer variable, then you again need a pointer to store it. Thus, when one pointer variable stores the address of another pointer variable, it is known as Pointer to Pointer variable or Double Pointer.

    int **p1;
    Here, we have used two indirection operator(*) which stores and points to the address of a pointer variable i.e, int *. If we want to store the address of this (double pointer) variable p1, then the syntax would become:
    int ***p2

    pointer to pointer example :-


    p2 contains the address of p (fff2) and p contains the address of number variable (fff4).

    #include <stdio.h>
    int main(){
    int number=50;
    int *p;//pointer to int
    int **p2;//pointer to pointer
    p=&number;//stores the address of number variable
    p2=&p;
    printf("Address of number variable is %x \n",&number);
    printf("Address of p variable is %x \n",p);
    printf("Value of *p variable is %d \n",*p);
    printf("Address of p2 variable is %x \n",p2);
    printf("Value of **p2 variable is %d \n",*p);
    return 0;
    }

    output :-

    Address of number variable is fff4
    Address of p variable is fff4
    Value of *p variable is 50
    Address of p2 variable is fff2
    Value of **p variable is 50

    #include <stdio.h>
    int main() {
    int a = 10;
    int *p1; //this can store the address of variable a
    int **p2;
    /* this can store the address of pointer variable p1 only.
    It cannot store the address of variable 'a'
    */ p1 = &a;
    p2 = &p1;
    printf("Address of a = %u\n", &a);
    printf("Address of p1 = %u\n", &p1);
    printf("Address of p2 = %u\n\n", &p2);
    // below print statement will give the address of 'a'
    printf("Value at the address stored by p2 = %u\n", *p2);
    printf("Value at the address stored by p1 = %d\n\n", *p1);
    printf("Value of **p2 = %d\n", **p2); //read this *(*p2)
    /* This is not allowed, it will give a compile time error-
    p2 = &a;
    printf("%u", p2);
    */ return 0;
    }

    output :-

    Address of a = 2686724
    Address of p1 = 2686728
    Address of p2 = 2686732
    Value at the address stored by p2 = 2686724
    Value at the address stored by p1 = 10
    Value of **p2 = 10

    Explanation of the above program :-



  • p1 pointer variable can only hold the address of the variable a (i.e Number of indirection operator(*)-1 variable). Similarly, p2 variable can only hold the address of variable p1. It cannot hold the address of variable a.
  • *p2 gives us the value at an address stored by the p2 pointer. p2 stores the address of p1 pointer and value at the address of p1 is the address of variable a. Thus, *p2 prints address of a.
  • **p2 can be read as *(*p2). Hence, it gives us the value stored at the address *p2. From above statement, you know *p2 means the address of variable a. Hence, the value at the address *p2 is 10. Thus, **p2 prints 10.




  • Previous Next

    Trending Tutorials




    Review & Rating

    0.0 / 5

    0 Review

    5
    (0)

    4
    (0)

    3
    (0)

    2
    (0)

    1
    (0)

    Write Review Here


    Ittutorial