Pointers in C Programming

Introduction

  • A variable which contains the memory location of another variable is known as a pointer.
  • It represents the location of the data item.
  • It is preceded by a '*' (asterik) before the variable.

  • Syntax:
    data_type *ptr_name;

  • A pointer variable can store only the address of a variable.

Concept of a pointer

Let us look at the following example:
int p = 4;
int *a;
a = &p;

Where,
p is the integer variable containing 4.
*a holds the address of p means a points to p.

concept of pointer

Example : Concept of pointer

#include<stdio.h>
void main()
{
     int p = 4;
     int *a;
     a = &p;
     printf("\n%d %u %u %d", p, &p, a, *a);
}


Output:
4      1237684756      1237684756     4

Note: The value of address will vary everytime we run a program as it takes whatever is stored in the memory.

Null Pointer

  • A null pointer is a special pointer value which does not point anywhere.
  • It also means that it does not point to any valid memory address.

  • Syntax:
    int *ptr = NULL;

  • A pointer variable can be checked for address storage of some variable or it contains a NULL.

  • Syntax:
    if (ptr == NULL)
    {
         Statement block;
    }

    A pointer can be initialized as a null pointer by using the constant 0 as follows:

    int ptr;
    ptr=0;


  • Both 0 and NULL can be used. But, it is better to use NULL to avoid ambiguity.
  • If a null pointer is tried to dereferenced then it will generate a run time error.

Pointers and Arrays

Wherever arrays are used pointers can be used over there.

Example:
int a[] = {10,20,30,40,50};

It will be stored in the memory in the following manner:

pointer array str

  • The notation of an array is a form of pointer notation.
  • The array name is the starting address of the array in the memory.
  • The base address is the address of the first element.
  • An error can occur if any attempt is made for changing the address of the array.

Example

Simple program for showing the relation between arrays and pointers. To find the average of 5 numbers

#include<stdio.h>
void main()
{
     float a, sum=0;
     int i;
     int score[] = {55,40,76,90,80};
     int *pt;
     pt = score;
     for(i=0;i<=4;i++)
     sum = sum+*(pt+i);
     a = sum/100.00;
     printf("\n Average score = %f", a);
}


Output:
Average score = 3.410000

Pointer Arithmetic

1. Increment-Decrement

Syntax:
int *pt;
pt++
pt--

pt++ will increment the pointer to the next location of the same type.
pt-- will decrement the pointer to the previous element of the same type.

2. Assignment
The pointers must be of the same type.

Syntax:
int *p1, *p2;

If we write *p1 = *p2 both the pointers will point to the same location.

3. Addition of an integer

Syntax:
int i, *p;

If we write p+i the pointer will point to the ith object after its initial position.

4. Subtraction of an integer

Syntax:
int i, *p;

If we write p-i the pointer will point to the ith object after its initial position.

5. Comparison of pointers
The pointers should be pointing to the elements of the same array.

Syntax:
int *p1, *p2;

If p1<p2 then 1 would be returned, if p1 points to an element which is close to the array beginning.

6. Subtraction of pointers
The pointers should be pointing to the elements of the same array.

Syntax:
int *p1, *p2;

If p1- p2 is there then it would return the number of elements between p1 and p2 but only if the condition is p1>p2.