Casting malloc to (void**)

lets say you are allocating memory for an integer variable then we use pointer to an integer int*

suppose if you want to allocate memory for pointer variable , then we use pointer to poniter int**

same goes for void* as well , hope below example helps you.

#include <stdio.h>
#include <stdlib.h>

int main()

    int iVar = 99;
    int *piVar;
    int **ppiVar;
    void *vptr;
    void **vpptr; // here void * also, enough, but we should know when we extract, what to extract 

    piVar = malloc(sizeof(int));
    *piVar = 10;

    ppiVar = malloc(sizeof(int *));
    *ppiVar = piVar;

    vptr = &iVar;
    vpptr = &vptr;

    printf("piVar = %d , ppiVar = %d vptr = %d and vpptr =%d \n", *piVar,**ppiVar, *(int* )vptr, **(int **)vpptr);
    return 0;

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top