Take the 2-minute tour ×
void fun(void *x)
    //what is the data type of 'x', since I could have passed float instead of 
    //  int so first I have to check datatype of this and then proceed further.
int main()
     int n=10;

You can see that we dont know the type of 'x' in function fun() so how could we find out that without using(passing) any other information in function argument?

share|improve this question
You can't, I'm afraid. Not in pure C. You'll need to pass additional information. –  Baldrick Oct 29 '13 at 14:14
C knows nothing more than that contained in the arguments type -- so in this case that it is a 'pointer to something'. There are some mechanisms in C++ to help but they aren't going to work when passing a simple 'int'. –  Rob Oct 29 '13 at 14:16
While not about void pointers, printf is a similar case. You send type information, in the form of a format string, to the function, so it can know the type of data it receives. –  Thomas Padron-McCarthy Oct 29 '13 at 14:52
add comment

5 Answers

up vote 2 down vote accepted

C11 has type-generic expressions to help you solve this problem. You could do something like this example:

#define myFunction(X) _Generic((X), \
    int *:   myFunctionInt,         \
    float *: myFunctionFloat,       \
    default: myFunctionInt          \

void myFunctionInt(int *x);
void myFunctionFloat(float *x);

And then call it the way you are:

int n = 10;


float n = 10.0f;

You will need to implement the separate target functions, but you could just do the pointer magic there and pass off to a common handler, for example.

share|improve this answer
TIL C11 has generics –  constantius Oct 29 '13 at 15:21
add comment

C does not have runtime type information. When you pass a pointer, then you pass a naked pointer - a memory address.

If you are using pointers to void but need to know the type, then you have to pass some additional information. Otherwise, you create different functions for different types, like foo that expects an int, dfoo that expects a double and so on.

share|improve this answer
add comment

You cannot know the type when you cast to a void *. You have to be careful while using a void pointer, because you can cast it to any type. That is the purpose to have a void pointer.

share|improve this answer
add comment

C does not allow to get the type of the variable passed by void*. The only thing, that you can do it is write kind of object oriented code by your self.

Probably something like this:

typedef struct generic_type_s 
    void* data;
    void  (*f)(void* data);
} generic_type_t;

void f_int(void* _data)
    int* data = (int*)_data;
    // Do something

int main()
   int data1 = 10;
   // Generic variable initialization
   generic_type_t gv;
   gv.data = &data1;
   gv.f = f_int;

   // Calling an appropriate function

   return 0;
share|improve this answer
add comment

The C way of doing this is using a tagged union:

typedef enum {
    // will default to TypeUnitialized if unspecified in structure initializer
    TypeUninitialized = 0,
    TypeFloat = 1,
} type_t;

typedef struct {
    type_t type;
    union {
        int u_int;
        float u_float;
} gen_val_t;

void fun(gen_val_t v)
    switch (v.type) {
    case TypeFloat:
        // process float
    case TypeInt:
        // process int
        // error

int main(int argc, char **argv)
    gen_val_t ov = { .type = TypeFloat,
                     .u_float = 3.45 };
share|improve this answer
ultimately You are passing extra information. –  Gaurav Jain Oct 29 '13 at 15:39
There's no way of getting this information at runtime except manual instrumentation like this. So it's in no way extra. –  constantius Oct 29 '13 at 16:35
add comment

Your Answer


