Pointers

Pointers

Published by: Nuru

Published date: 21 Jun 2021

Pointers Photo

Pointers

A pointer is a variable that stores the address of another variable. Memory can be visualized as an ordered sequence of consecutively numbered storage locations. A data item stored in memory in one or more adjacent storage locations depends upon its type. That is the number of memory locations required depends upon the type of variable. The address of a data item is the address of its first storage location. This address can be stored in another data item and manipulated in a program. The address of a data item is a pointer to the data item, and a variable that holds the address is called a pointer variable. Some uses of pointers are:

  1.  Accessing array elements.
  2.  Returning more than one value from a function.
  3. Accessing dynamically allocated memory.
  4. Implementing data structures like linked lists, trees, and graphs.

Address (&) Operator and indirection (*) operator

C provides an address operator "&", which returns the address of a variable when placed before it. This operator can be read as “the address of”, so &age means the address of age, similarly &sal means address of sal. The following program prints the address of variables using the address operator. We can access a variable indirectly using pointers. For this, we will use the indirection operator (*). By placing the indirection operator before a pointer variable, we can access the variable whose address is stored in the pointer.

&: Address of
* : value at address of

Program to understand & and * operator.
#include
#include
void main()
{
int age=25,*page;
float sal=50000,*psal;
page=&age;
psal=&sal;
printf("\nAddress of age=%u and value of age=%d",page,*page);
printf("\nAddress of sal=%u and value of sal=%f",psal,*psal);
getch();
}

Declaration of pointer

Like all other variables, it also has a name, has to be declared, and occupies some space in memory. It is called pointer because it points to a particular location in memory by storing the address of that location.

Syntax: datatype *identifier

Example:
int age; //declaring a normal variable
int *ptr; //declaring a pointer variable
ptr=&age; //store the address of the variable age in the variable ptr.

  • Now ptr points to age or ptr is a pointer to age.
  • We've created a pointer ptr which becomes a variable that contains the address of another variable age

Pointers

 

Pointer arithmetic

All types of arithmetic operations are not possible with pointers. The only valid operations that can be performed are as follow:

  1. Addition of an integer to a pointer and increment operation.
  2. Subtraction of an integer from a pointer and decrement operation.
  3. Subtraction of a pointer from another pointer of the same type.

Pointer arithmetic is somewhat different from ordinary arithmetic. Here all arithmetic is performed relative to the size of the base type of pointer. For example, if we have an integer pointer pi which contains address 1000 then on incrementing we get 1002 instead of 1001. This is because the size of the int data type is 2.Similarly, on decrementing pi, we will get 998 instead of 999. The expression (pi + 3) will represent the address 1006.

Program to understand pointer arithmetic
#include
#include
void main()
{
int a=5,*pi;
float b=7.3,*pf;
char c='x',*pc;
clrscr();
pi=&a;
pf=&b;
pc=&c;
printf("\n Address of a=%u ",pi);
printf("\n Address of b=%u ",pf);
printf("\n Address of c=%u ",pc);
pi++;
pf++;
pc++;
printf("\n\n Address of a=%u ",pi);
printf("\n Address of b=%u ",pf);
printf("\n Address of c=%u ",pc);
getch();
}

Output:

Pointers

 

The arithmetic operations that can never be performed on pointers are:

  1. Addition, multiplication, division of two pointers.
  2. Multiplication between pointer and any number.
  3. Division of a pointer by any number.
  4. Addition of float or double values to pointers.

Pointer to pointer (double indirection)

The pointer variable itself might be another pointer so pointer which contains another pointer‟s address is called pointers to pointers or multiple indirections.

Syntax: datatype **identifier;

Example:
int **pptr;

Program to understand pointer to pointer
#include
#include
void main()
{
int a=5,*pa,**ppa;
clrscr();
pa=&a;
ppa=&pa;
printf("\nValue of a=%d",*pa);
printf("\nValue of a=%d",**ppa);
getch();
}

Pointers

Output:

Value of a=5

Value of a=5