What is pointer in c give an example with explanation

pushpakumari | 429 Views | c programming | 01 Sep 2016

 

C Pointer is a variable that stores/points the address of another variable. C Pointer is used to allocate memory dynamically i.e. at run time. The pointer variable might be belonging to any of the data type such as int, float, char, double, short etc.

Syntax :

data_type *var_name; 

Where, * is used to denote that “var_name” is pointer variable and not a normal variable.

Example :

int *a;

char *b;

float *c;

Sample Program :

#include <stdio.h>
#include <string.h>
#include <conio.h>
void main ( )
{
int x;
int *ptr;
x=10;
ptr=&x;
printf("\n The value of x =%d",x);
printf("\n The value of *ptr=%d",*ptr);
printf( "\n Address of ptr=%d", ptr);
getch();
}

Sample Output :

The value of x= 10
The value of *ptr= 10
Address of ptr= -12

Features of pointers :

  • Pointer are efficient in handling data.
  • Used for saving memory space.
  • Reduce length and complexity of the program.
  • Since it access the address ,the execution time will be take very less amount.

Advantages Of Pointer :

  • More compact and efficient code.
  • Used to achieve simplicity & clarity.
  • Allows us to accessed the memory directly . 

Method Of Calling Pointers :

Pointer to Pointer :

A pointer can point to another pointer (which means it can store the address of another pointer), such pointers are known as double pointer OR pointer to pointer.

Passing pointers to function :

Pointers can also be passed as an argument to a function, using this feature a function can be called by reference as well as an array can be passed to a function while calling.

Function pointers :

 A function pointer is just like another pointer, it is used to store the address of a function. Function pointer can also be used to call a function in C program.