Pointer in C Definition, Syntax & Working with Advantages & Disadvantages

Definition: In simple words pointer is a variable that stores address (memory location) of another variable's. This means that a pointer holds the memory address of another variable.



   There are two main operator's used in pointer:


1. Reference operator & 
2. Dereference operator *


1. Reference operator (&):


 The & is a unary operator that returns the memory address of its operand.



   Syntax of reference operator &:

&variable-name;

 
  • For Example:

If you have any variable var in your program, &var will give you it's memory address.

We have already used address(&) numerous times while using the scanf() function:

scanf("%d",&var);




  Examples program to print address of variable:


Here, %p is format specifier which is used to print the address of a pointer.
 

#include<stdio.h>
int main()
{
// declare variable 
int var;

printf("Address is: %p\n", &var);

return 0;
}


Above program shows the following output:

Pointer in C Definition, Syntax & Working with Advantages & Disadvantages



2. Dereference operator (*):


The * is a unary operator that returns the value of the variable at the address specified by its operand.

   Syntax of dereference operator (*):

*memory-address or pointer-variable;




  Example program of dereference operator (*):


#include<stdio.h>
int main()
{
int var=10;
/* It prints value pointed by (&var)*/
printf("Pointed value is: %d",*(&var));
return 0;
}

Above program shows the following output:

Example program of dereference operator



   Declaration syntax of pointer in C:



This is the general syntax of pointer. 

  • Here, * symbol specifies it is a pointer variable. You must prefix * before declare varible to it is pointer.
  • pointer_variable_name is the name of pointer variable.


Datatype of a pointer must be same as the data type of the variable to which the pointer variable is pointing.


  • For Example:

int *i; // pointer to intiger variable
char *c; //pointer to character variable
float *f; // pointer to float variable



  Example Program of Pointer:


#include <stdio.h>
int main()
{ 

    int num = 10;
    int *ptr = &num;
    // Here,  pointer points the value of num

    printf("Value of num = %d \n", num);   
 printf("Address of num = %x \n\n", &num);

    printf("Value of ptr = %x \n",ptr);
 printf("Address of ptr = %x \n", &ptr);
   

 return 0;

}


In this program %x is used to print hexadecimal values.


Above program shows the following output:

Example program of pointer




  Advantages of using pointer:


1. Pointer provide direct memory access.

2. It reduces the execution time of the program.

3. It reduces the storage space and complexity of the program.

4. Pointer provides an alternate way to access array elements.

5. Using pointers, arrays and structures can be handled in more efficient way.



  Disadvantages of using pointer:


1. In pointer if sufficient memory is not available for the storage of pointers during runtime, the program may crash.

2. Pointers are slower than normal variables.

3. Uninitialized pointers might cause segmentation fault.


Learn Pointers with more Examples....

No comments:

Post a Comment

If you have any doubts, please discuss here...👇