Pointer in c

A Pointer in C language is a variable which holds the address of another variable of same data type.Pointers are used to access memory and manipulate the address.Pointers are one of the most distinct and exciting features of C language. It provides power and flexibility to the language. Although pointers may appear a little confusing and complicated in the beginning, but trust me, once you understand the concept, you will be able to do so much more with C language.

Before we start understanding what pointers are and what they can do, let's start by understanding what does "Address of a memory location" means?

Pointer

Address in C

Whenever a variable is defined in C language, a memory location is assigned for it, in which it's value will be stored. We can easily check this memory address, using the & symbol.

If var is the name of the variable, then &var will give it's address.

Let's write a small program to see memory address of any variable that we define in our program.

 #include<stdio.h>
  void main() 
{
   int var = 7;
   printf("Value of the variable var is: %d", var);
   printf("Memory address of the variable var is: %x", &var);
}

Output

   Value of the variable var is: 7
   Memory address of the variable var is: bcc7a00
  

There are two symbol used in pointer - & and *.Let us Understand the use of both symbol briefly with example

Example1

#include<stdio.h>
void main()
{
  int x,iptr;
  printf(***Testing pointer variable***);
  x=10;
  iptr=&x;
  printf("%d",iptr);
}

Output

 **Testing Pointer Variables**
  1000

Example2

 #include<stdio.h>
void main()
{
   int x,*iptr;
   printf(***Testing pointer variable***);
   x=10;
   iptr=&x;
   printf("Address %d holds value %d",iptr,*iptr);
}
 

Output

  **Testing Pointer Variables** 
 Address 1000 holds value 10
 

Benefits of using pointers

  1. Pointers are more efficient in handling Arrays and Structures.
  2. Pointers allow references to function and thereby helps in passing of function as arguments to other functions.
  3. It reduces length of the program and its execution time as well.
  4. It allows C language to support Dynamic Memory management.