C Pointers


The pointer in C language is a variable, it is also known as locator or indicator that points to an address of a value.


Advantage of pointer

Pointer reduces the code and improves the performance, it is used to retrieving strings, trees etc. and used with arrays, structures and functions.

We can return multiple values from function using pointer.

It makes you able to access any memory location in the computer's memory.


Usage of pointer

  • Dynamic memory allocation
  • we can dynamically allocate memory using malloc() and calloc() functions where pointer is used.

  • Arrays, Functions and Structures
  • Pointers in c language are widely used in arrays, functions and structures. It reduces the code and improves the performance.

Symbols used in pointer

SymbolNameDescription
& (ampersand sign)address of operatordetermines the address of a variable.
* (asterisk sign)indirection operatoraccesses the value at the address.

Address Of Operator

The address of operator '&' returns the address of a variable. But, we need to use %u to display the address of a variable.

Example

#include <stdio.h>      
#include <conio.h>    
void main(){      
int num =50;    
clrscr();      
printf("value of number is %d, address of number is %u",num,&num);  
getch();      
}      
Output
value of number is 50, address of number is fff4

Declaring a pointer

The pointer in c language can be declared using * (asterisk symbol).

int *a; //pointer to int  
char *c;  //pointer to cha

Example

#include <stdio.h>      
#include <conio.h>    
void main(){      
int number=50;  
int *p;    
clrscr();  
p=&number;//stores the address of number variable  
      
printf("Address of number variable is %x \n",&number);  
printf("Address of p variable is %x \n",p);  
printf("Value of p variable is %d \n",*p);  
  
getch();      
}
Output
Address of number variable is fff4
Address of p variable is fff4
Value of p variable is 50

NULL Pointer

A pointer that is not assigned any value but NULL is known as NULL pointer.

Example

int *p=NULL; //The value of pointer is 0 (zero).

Share this article on