Pointer-in-c .

In this session i have discussed pointer in C .


Pointer is a variable that store the address of other variable .


Key features of pointer

1-Normal variable store the value where the pointer variable store the address of other variable .

2-C pointer always initialized null value .

3-& symbol is used to get the address of a variable .

4-* symbol is used to get the value from pointer .

5-The pointer variable belong to any data type such as int , float , char , double etc .


Advantages of pointer

1-Pointer is used to reduced the length and complexity of program .

2-Pointer increase the execution speed .

3-Pointer is more efficient to handle the data tables .

4-The use of a pointer array of character strings results in saving of data storage space in memory.

5-You can pass the function as a parameter with the help of function pointer .

6-You can allocate the memory dynamically with the help of pointer .

7-Pointers are used to construct different data structures such as linked lists, queues, stacks, etc.


Disadvantages of pointer

1-Uninitialized pointers might cause error occured .

2-Debugging is not easy in pointer .

3-Pointers are slower than normal variable .

4-Crash is common issues with pointer .

5-Pointer syntax is realy very complicated . When they have started pointer to pointer to pointer to array of function .


Get the address of a variable

Before know the pointer first you have need to know how to get address of a variable . You will need to use the & symbole with variable like &x display the address of x variable .

Example : Write a program that get the address of a variable .

    #includ<stdio.h> 
    int main()
    {  
          int x;
          x=20;
          printf("Value : %d",x);
          printf("Address : %u",&x);//print the address of x variable
          return 0;
    }
    
    OUTPUT : Value : 20
             Address : 2686732
    


Declaring pointer variable

You will use *(asterisk) to declare the pointer variable as follow .

   data_type*ptr_name
   int*x;//pointer to int
   char*ch;//poiter to char
   

In the above statement the x is pointer variable of type int .


Example : Write a program that demonstrate the use of pointer variable .

    #includ<stdio.h> 
    int main()
    {  
          int*ptr, x;
          x=10;

          printf("Address of x : %u\n",&x);
          printf("Value of x : %d\n",x);
          ptr=&x;
          printf("Address of ptr : %u\n",ptr);
          printf("Value of ptr : %d\n",*ptr);
          return 0;
    }
    
    OUTPUT : Address of x : 2686732
             Value of x : 10
             Address of ptr : 2686732
             Value of ptr : 10
    


Explanation of above program

First of all i have created pointer variable ptr and normal variable as x both are int type . The x variable contains the garbage value and ptr contains the garbage address .

In next we have assign the x variable as 10 value . That means the x address contains the 10 value .

We have print the address and value of x variable using printf() .

Next we have assign the address of x variable in ptr as ptr=&x .

That means the ptr contains the address of x variable and x variable address contains the value 10 .

When we have print the address and value of ptr then it will print the same address and value .


Welcome to AndroidATPoint.

back image                     next image