Pointers in C language

  • Pointers in C language is a variable that stores/points the address of another variable. A Pointer in C is used to allocate memory dynamically i.e. at run time. The pointer variable might be belonging to any of the data types such as int, float, char, double, short etc.
  • Pointer Syntax : data_type *var_name; Example : int *p;  char *p;
Example:-
int *ptr;
  • Where * is used to denote that “p” is pointer variable and not a normal variable.
  • int a = 5;
    int *ptr = NULL;
    
    ptr = &a;
    

  • This assigns the value of the address of a to ptr. For example, if a is stored at memory location of 0x8130 then the value of ptr will be 0x8130 after the assignment. To dereference the pointer, an asterisk is used again:
Let's see an example program

#include <stdio.h>

int main () {

   int  var1;
   char var2[10];

   printf("Address of var1 variable: %x\n", &var1  );
   printf("Address of var2 variable: %x\n", &var2  );

   return 0;
}
    When the above code is compiled and executed,
    the output will be like

    Address of var1 variable: bff5a400
    Address of var2 variable: bff5a3f6


    What are Pointers?

    pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. Like any variable or constant, you must declare a pointer before using it to store any variable address. 
    int    *ip;    /* pointer to an integer */
    double *dp;    /* pointer to a double */
    float  *fp;    /* pointer to a float */
    char   *ch     /* pointer to a character */

    How to Use Pointers?

    #include <stdio.h>
    
    int main () {
    
       int  var = 20;   /* actual variable declaration */
       int  *ip;        /* pointer variable declaration */
    
       ip = &var;  /* store address of var in pointer variable*/
    
       printf("Address of var variable: %x\n", &var  );
    
       /* address stored in pointer variable */
       printf("Address stored in ip variable: %x\n", ip );
    
       /* access the value using the pointer */
       printf("Value of *ip variable: %d\n", *ip );
    
       return 0;
    }
    When the above code is compiled and executed,the output will be like
    Address of var variable: bffd8b3c
    Address stored in ip variable: bffd8b3c
    Value of *ip variable: 20

    NULL Pointers

    It is always a good practice to assign a NULL value to a pointer variable in case you do not have an exact address to be assigned

    #include <stdio.h>
    
    int main () {
    
       int  *ptr = NULL;
    
       printf("The value of ptr is : %x\n", ptr  );
     
       return 0;
    }
    When the above code is compiled and executed, 
    the output will be like,
    The value of ptr is 0
    

    To check for a null pointer, you can use an 'if' statement as follows −
    if(ptr)     /* succeeds if p is not null */
    if(!ptr)    /* succeeds if p is null */



    Sr.No.Concept & Description
    1Pointer arithmetic
    There are four arithmetic operators that can be used in pointers: ++, --, +, -
    2Array of pointers
    You can define arrays to hold a number of pointers.
    3Pointer to pointer
    C allows you to have pointer on a pointer and so on.
    4Passing pointers to functions in C
    Passing an argument by reference or by address enable the passed argument to be changed in the calling function by the called function.
    5Return pointer from functions in C
    C allows a function to return a pointer to the local variable, static variable, and dynamically allocated memory as well.

    Comments