×
☰ Menu

Pointers in C

A pointer is a variable that stores the address of another variable, i.e., direct address of the memory location. Like any variable or constant, you must declare a pointer before using it to store any variable address.

The general form of a pointer variable declaration is −

data-type *pointer-name;

 

Here, data-type is the pointer's base type; it must be a valid C data type and pointer-name is the name of the pointer variable. The asterisk * that is used to declare a pointer is the same as the asterisk that is used to multiply. The asterisk, on the other hand, is used to indicate a variable as a pointer in this statement. Take a look at a few examples of proper pointer declarations.

int    *iptr;    /* pointer to an integer */

double *dptr;    /* pointer to a double */

float  *fptr;    /* pointer to a float */

char   *chptr     /* pointer to a character */

 

In a C program, pointers are used to manipulate addresses and access memory. Dereferencing a pointer is the process of getting the data stored at the location where the pointer references in memory. In a similar vein, one might think of a page number in a book's index as a pointer to the relevant page; dereferencing such a pointer is getting the content from the specified page number.

Advantages of Pointers

The following are the advantages of using pointers:

  • Pointers provide effective memory access.
  • Run-time memory allocation and deallocation.
  • It provides a way to return multiple data items from a function using its function arguments
  • Writing effective programs is advantageous.
  • It deals specifically with hardware parts.
  • Additionally, it offers a different method of accessing an array element.
  • It helps to direct memory access.