Pointers in C Programming (Declaration, Referencing & Dereferencing)

Pointer is an important feature of C programming language. It is considered as the beauty of C programming language making this language more powerful and robust.

A pointer is a special variable in C programming language which stores the memory address of other variables of the same data type. As a pointer is variable, it is also created in some memory location.

Declaration of Pointer Variable

Like normal variables, pointer variables must be declared before using them. General syntax for declaring pointer variable is:

data_type      * pointer_name;

Here, data_type can be any valid C data types and pointer_name can be any valid C identifier.

Examples of Declaration of Pointer:

  1. int *ptr; Here ptr is a pointer variable and it is read as a pointer to integer since it can point to integer variables.
  2. float *fptr; Here fptr is a pointer variable and it is read as a pointer to float since it can point to float variables.
  3. char *cp; Here cp is a pointer variable and it is read as a pointer to character since it can point to character variables.

Referencing of Pointer (Initialization of Pointer)

Making a pointer variable to point other variables by providing address of that variable to the pointer is known as referencing of pointer.

It is also known as initialization of pointers. For proper use of pointer, pointer variables must point to some valid address and it is important to note that without referencing, pointer variables are meaningless.

General syntax for referencing of pointer is:

pointer_variable = &normal_variable;

Here pointer_variable and normal_variable must be of the same data types.

Examples of Referencing of Pointer:

  1. 
    int a=5;
    int *ptr;
    ptr = &a;
    
    
    Here pointer ptr got address of variable a so, pointer ptr is now pointing to variable a.
  2. 
    float  val=5.5;
    float *p;
    p = &val;
    
    
    Here pointer p got address of variable val so, pointer p is now pointing to variable val.

    But!


    
    float x=30.4;
    int *iptr;
    iptr = &x;
    
    
    is invalid!!! Because pointer iptr cannot store address of float variable.

Dereferencing of Pointer

So far, the operator * (star) used in front of the name of the pointer variable is known as pointer or dereferencing or indirection operator. After valid referencing of pointer variable, * pointer_variable gives the value of the variable pointed by pointer variable and this is known as dereferencing of pointer. For the sake of simplicity, *pointer_variable after referencing instructs compilers that go to the memory address stored by pointer_variable and get value from that memory address.

Examples of Dereferencing of Pointer:

  • 
    int a=5;
    int *ptr;
    ptr = &a;
    printf(“a = %d”, *ptr);
    
    
    This prints a = 5 which is accessed through pointer.
  • 
    float  val=5.5;
    float *p;
    p = &val;
    printf(“val = %f”, *p);
    
    
    This prints val = 5.5 which is accessed through pointer. .

Pointer Examples

1. Program to illustrate concept of declaration, referencing, dereferencing of pointer and displaying address

C Source Code: Pointer Example


#include<stdio.h>

int main()
{
 int a=50, *p; 
 /* Here a is normal variable and *p is pointer variable */

 p = &a; /* Referencing */
 /* After above statement i.e. referencing, now pointer points to a */ 
 
printf(“Value of a = %d\n”, a);
printf(“Address of a = %u\n”, &a);
printf(“Value of a using pointer = %d\n”, *p);
printf(“Address of a using pointer = %u\n”, p);
return 0;
}

Output

Value of a = 50
Address of a = 65524
Value of a using pointer = 50
Address of a using pointer = 65524

Above address will be different on your system.

2. Program to illustrate concept of declaration, referencing and dereferencing of pointer:

C Source Code: Pointer Example


#include<stdio.h>

int main()
{
 int a=50, *p;
 float val = 50.5, *f;

 p = &a;
 f = &val;
 printf(“Value of a = %d\n”, *p);
 printf(“Value of val = %d\n”, *f)

 return 0;
}

Output

Value of a = 50
Value of val = 50.5