Loading...
Loading...
00:00:00

What Is Pointer?

Pointer is a variable that can hold address of another variables, functions or pointers of same data types. To create a pointer variable we have to use * before variable name. This * can be use for dereference of pointer mean get the actual value at that address. for assigning address in pointer variable we have to use this & address-of(reference) operator.

Here is the syntax for declare a pointer variable and assigning value in pointer variable and get the variable value.

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char const *argv[])
{
    int num = 20; // int type variable
    int *x;       // int type pointer variable
    x = &num;     // assigning address of num variable using & operator
    printf("The value of num varible is %d", num);
    printf("The address of num variable is %d", x);
    printf("The value at address %d is %d", x, *x); // getting value at address using * operator
    return 0;
}

Application Of Pointer

In this example, we swapped two variables values using pointers. Note onething, when we call the function we passed addressess of variables not variables values this is the important thing you have to remember.

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char const *argv[])
{
    int num0 = 20; 
    int num1 = 50; 
    void swap_number(int *n1, int *n2)
    {
        int temp;
        temp = *n1;  // access the value at address 'n1'
        *n1 = *n2;   // access the value at address 'n2'
        *n2 = temp;
    } 
    swap_number(&num0,&num1);  // call the function
    printf("The value of num0 = %d and num1 = %d
",num0,num1);
    return 0;
}

Function Pointer

A pointer can store a address of function.

Syntax of create a function pointer

return_type (*function_pointer_name)(parameters);  // declare a function pointer
function_pointer_name = function_name;   // assign address in function pointer
function_pointer_name();            // call the function using function pointer

Example In this example , function is without parameters and no return type.

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char const *argv[])
{
    void fun()  // function 
    {
        printf("Hello World");
    }
    void (*fun_p)();  // declare a function pointer
    fun_p = fun;     // assign address in pointer
    fun_p();         // call the function using pointer
    return 0;
}

Function Pointer With Parameters(Arguments)

function pointer with parameters In this example, function is taking arguments .

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char const *argv[])
{
    void fun(int x)
    {
        printf("The square of %d is %d", x, x * x);
    }
    void (*fun_p)(int); // declare a function pointer
    fun_p = fun;        // assign the address of function in function pointer
    fun_p(10);          // call the function using function pointer
    return 0;
}

Function Pointer With Parameters And Return Type

Example - In this example, function takes parameters and return a value.

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char const *argv[])
{
    int fun(int x) // function
    {
        return (x * x);
    }
    int (*fun_p)(int);                       // declare a function pointer
    fun_p = fun;                             // assign the address of function in function pointer
    printf("The square is %d", fun_p(10)); // call the function using function pointer and get the value
    return 0;
}

Pointer To Pointer

A pointer can hold the address of another pointer variable.

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char const *argv[])
{
    int x = 10;
    int *ptr1;    // first level pointer
    int **ptr2;   // second level pointer
    ptr1 = &x;    // assign variable address in  ptr
    ptr2 = &ptr1; // assign pointer address in pointer variable
    printf("The value of x is %d", x);
    printf("The value of *ptr1 is %d", *ptr1);
    printf("The value of ptr2 is %d", **ptr2); // call the function using function pointer and get the value
    return 0;
}