March 28, 2019

Pointers in C

Previously, we learnt about strings in C, but in this article, we are going to know about pointers in C. Every variable that we declare need some address to be stored. Pointers serve this very purpose.

Pointers in C

Overview:


Definition of pointer

A pointer is referred to any variable whose value is the address of another variable, i.e., exact address of the memory location. One must declare a pointer before using it to store any variable address, like it is done while declaring variables or constants.



Declaration of pointer

Pointer variable is usually declared in the following manner:

type *var-name;

Here, type is the pointer's base type; it must be a valid C data type and var-name is the name of the pointer variable. The asterisk (*) that is used to declare a pointer is the same asterisk used for multiplication. However, in this statement the asterisk is  used to designate a variable as a pointer. Take a look at some of the valid pointer declarations −

int    *ip;    /* pointer to an integer */
double *dp;    /* pointer to a double */
float  *fp;    /* pointer to a float */
char   *ch     /* pointer to a character */

The data type of the value of all pointers, (int/float/char), is the same, a long hexadecimal number that represents a memory address. The pointers of different data types are differentiated by the data type of the variable or constant that the pointer points to.

How To Use Pointers in C?

There are some important tasks, where we take the help of pointers quite frequently.
  1. A pointer variable is defined,
  2. The address of a variable to a pointer is assigned, and
  3. Finally, the value is accessed at the address available in the pointer variable.

How Pointers Work in C?
How Pointers Work in C?

This is carried out with the help of unary operator, asterisk (*), that returns the value of the variable located at the address specified by its operand. The following program will clarify the use of pointers in C:

#include<stdio.h>
int main()
{
   int var=20;   /*actual variable declaration*/
   int *ip;        /*pointer variable declaration*/
   ip=&var;  /*
store address of var in pointer variable*/
   printf("Address of var variable: %x\n", &var);
   /*address stored in pointer variable*/
   printf("Address stored in ip variable: %x\n", ip);
   /*access the value using the pointer*/
   printf("Value of *ip variable: %d\n", *ip );
   return 0;
}

Output:

Output of Pointer

NULL Pointers

When one doesn't have exact address to be assigned, a NULL value is assigned to the pointer variable. This assignment is done at the time of variable declaration. Any pointer that is assigned to NULL is called a null pointer.



The NULL pointer is a constant whose value is zero and it is defined in several standard libraries. Observe the following program:

#include <stdio.h>
int main()
{
   int *ptr=NULL;
   printf("The value of ptr is : %x\n", ptr  );
   return 0;
}

Ouput:

Output of NULL pointer

In most of the OSs, programs are not allowed to gain memory access to address 0 because that memory is reserved by the OS. The memory address 0 has a special significance. It indicates that the pointer is not intended to point to an accessible memory location. But conventionally, if a pointer contains the null (zero) value, it is assumed to point to nothing.

In order to check for a null pointer, one can use the 'if' statement as follows:

if(ptr)     /* succeeds if p is not null */
if(!ptr)    /* succeeds if p is null */

Benefits of pointer

Now that we have known about pointers, what are its advantages in C programming?
  • Pointers allow an efficient passage of arrays and strings to functions.
  • Pointers make it possible to return multiple value from the function.
  • Pointers cut down the size and complexity of a program.
  • Pointers increase the processing speed.
  • Pointers save the memory.

All programs written in this post are compiled online.

Books I Prefer:



  

I hope this article was helpful to understand pointers in C! Comment below, if you've got any question. Head back soon for another interesting article on C Programming.