Pointers in c programming | pointers in c with Examples

 Pointers is an important concept to learn in c programming. Pointers are used to access the address of the variables. Every variable in c programming has a memory location i.e it stores the specific values of the variable in particular location in the memory and the memory of the variable can be accessed by using the 'address of operator' (ampersand-&).


Pointers in c progtamming | pointers in c with Examples
Pointers in c programming 


Pointer is a variable which is used to store the variables address. Before using the pointer we must declare the pointer to store the variable address.

Syntax


datatype *variable_name; 
Here datatype is the type of the pointer like int, float ..etc. variable_name is the name of the pointer variable and the * is used to declare a pointer and it is called as indirection operator which is used to access the value stored in the address. 

Note:- This asterik is not same as we use while multiplying the two operands, here it specify the pointer variable.


Examples:-


int *var;  

float *var;  

char *var;  

Example program to print the address of the variables:-


In this program we are printing the address of the variables. ('%x' is used to print the hexadecimal value)

#include <stdio.h>

int main()
{
    printf("Program to print the address of the variable\n");
    
    int num1=100;
    float num2 =34;
    
    printf("Address of variable num1 = %x\n", &num1);
    printf("Address of variable num1 = %x\n", &num2);

    return 0;
}

Output:-
Program to print the address of the variable
Address of variable num1 = e4a8aca8
Address of variable num1 = e4a8acac
Address of variable num1 = -458707800

Example program for how to use the pointers:-

In this program first we declare a pointer named point and storing the address of the num1 variable into the pointer and now the pointer contains the address of num1 variable and the value is same.

#include <stdio.h>

int main()
{
    printf("Program to print the address of the variable\n");
    
    int num1=100;
    int *point;
    
    point = &num1;
    
    printf("Address of variable num1 = %x\n", &num1);
    
    printf("Value of variable num1 = %d\n", num1);
    
    printf("Address of pointer variable point = %x\n", point);
    
    printf("Value of the pointer variable point = %d\n", *point);
    

    return 0;
}

Output:-
Program on pointers
Address of variable num1 = 78676134
Value of variable num1 = 100
Address of pointer variable point = 78676134
Value of the pointer variable point = 100
If we change the value of the pointer than it will also change the value of the original variable since the pointer stores the address of the original variable. See the example below


#include <stdio.h>

int main()
{
    printf("Program on pointers\n");
    
    int num1=100;
    int *point;
    
    point = &num1;
    
    printf("Address of variable num1 = %x\n", &num1);
    
    printf("Value of variable num1 before adding 100 = %d\n", num1);
    
    printf("Address of pointer variable point = %x\n", point);
    
    printf("Value of the pointer variable point before adding 100 = %d\n", *point);
    
    *point = *point + 100;
    
    printf("Value of variable num1 after adding 100 = %d\n", num1);
    
    printf("Value of the pointer variable point after adding 100 = %d\n", *point);

    return 0;
}

Output:-
Program on pointers
Address of variable num1 = 46a631b4
Value of variable num1 before adding 100 = 100
Address of pointer variable point = 46a631b4
Value of the pointer variable point before adding 100 = 100
Value of variable num1 after adding 100 = 200
Value of the pointer variable point after adding 100 = 200

Null pointer:-

It is always a good practice to assign null to a pointer variable.

#include <stdio.h>

int main()
{
    printf("Null  pointers\n");
    
    int *point;
    
    point = NULL;
    
    printf("Value of Pointer = %d\n", point);
    
   

    return 0;
}

Output:-
Null  pointers
Value of Pointer = 0

Post a Comment

0 Comments