Dereferencing Pointers and Void Pointers in C

Introduction

Dereferencing is an operation performed to access and manipulate data contained in the memory location pointed to by a pointer. The operator * is used to dereference pointers. A pointer variable is dereferenced when the unary operator *, in this case, called the indirection Operator, is used as a prefix to the pointer variable or pointer expressions. Any operation performed on the dereferenced pointer directly affects the value of the variable it points to.

Source Code

#include<stdio.h>
#include<conio.h>

void main() {
    int *iptr, var1, var2;
    iptr = &var1; // initialize pointer iptr
    *iptr = 25; // dereferencing iptr , var1=25
    *iptr += 10; // var1=var1+10
    printf("Variable Var1 contains: %i\n", var1);
    var2 = *iptr; // same as var2=var1 , next statement prints var2, it also contains 35
    printf("Variable var2 contains: %i\n", var2);
    iptr = &var2;
    *iptr += 20; // increase contents of var2 by 20, next statement prints var2, it now contains 55
    printf("Variable var2 now contains: %i\n", var2);
    getch();
}

Output

Output

Code Explanation

  • The reference and Dereference operators are thus complementary.
  • & Is the address of the operator and can we read it simply as the address of.
  • *Is the dereference operator and can be read as the value pointed to by.
  • They have sort of opposite meanings. An address obtained with & can be referenced with *.

Void Pointers

Pointers defined to be of a specific data type cannot hold the address of any other type of variable. It is syntactically incorrect in C to assign the address of, say, an integer variable to a pointer of type float, as illustrated below.

float *f_ptr;//declare pointer to float
int my_int;//declare an integer
--------------
--------------
f_ptr=&my_int;// This statement result in error during compilation.

An Exception to this restriction is a general-purpose pointer type called the void pointer. The format for declaring a void pointer is as follows.

void *v_ptr;

This declaration uses the C-reserved word void for specifying the type of pointer. Pointers declared in this manner do not have any type associated with them and can contain the address of any type of variable (int, char, float).

Valid C statements in Void Pointer

void *vd_ptr;
int *it_ptr;
char chvar;
float flvar;
double dlvar;

vd_ptr = &invar; // a valid pointer assignment
vd_ptr = &chvar;
vd_ptr = &flvar;
vd_ptr = &dlvar;
it_ptr = &invar;
// it_ptr = &chvar; // an invalid assignment

Pointer to void cannot be directly dereferenced. Like other pointers, variables use the indirection operator. Prior to differencing a pointer to void It must be suitably type cost to the required data type. A void pointer may be typecast by preceding its name with a relevant data type, followed by an * both of which are enclosed within a pair of parenthesis.

Illustration of void pointers

#include<stdio.h>
#include<conio.h>

void main()
{
    int i1 = 100;
    float f1 = 200.5;
    void *vptr;
    vptr = &i1;
    clrscr();
    printf("i1 contains %i\n", *((int *)vptr));
    //dereferencing a void pointer using the type cast (int *)
    vptr = &f1;
    printf("f1 contains %f\n", *((float *)vptr));
    //dereferencing a void pointer using the type cast (float*)
    getch();
}

Output

Contains

Summary

Dereferencing pointers is fundamental in C programming, especially when dealing with dynamic memory allocation, arrays, and accessing data indirectly. It allows you to manipulate and work with the data stored in memory through pointers.

Void pointers provide flexibility in handling different data types within the same code. However, their use requires caution because they sacrifice type safety, and improper dereferencing can lead to undefined behavior.


Similar Articles