mercredi 13 mai 2015

How to typecast void pointer based on condition?

I want to initialize a pointer based on if-else condition as shown below. So i initialize as void and based on condition i make it to int/float. But i run into errors. It is a basic C code...how to overcome this?

//***Code

#include <stdio.h>

int main(void)
{

    int b   = 99;
    float c = 46.5;
    void *a;

    if(1)
    {
        int *a;
        a = &b;
    }
    else
    {
        float *a;
        a = &c;
    }

    printf("Value is %d", *a);


    return 0;

}

//***Error

error: invalid use of void expression printf("b is %f", *a)

Aucun commentaire:

Enregistrer un commentaire