Rohan
Rohan

Reputation: 1352

Possible to check return type of a function?

Let's say I have a function..

void * getValue(...);

Is there a way that I check for the return type of a call to getValue(...) ?

I plan on using a void* method such as getValue() in a program and the process of my program will be dependent on the return type of getValue().

Is it possible to check for return type?

Upvotes: 0

Views: 1775

Answers (3)

nobody
nobody

Reputation: 20174

No - C does not tag values with their type, so the information is not available.

Possible alternatives include adding an out parameter to the function to indicate the type, or making/using a variant struct that includes both the value and an enum indicating its type.

Upvotes: 2

pmg
pmg

Reputation: 108938

You may want to return a structure instead

enum valuetype {PCHAR, PSHORT, PINT, PLONG, PLLONG, PFLOAT, PDOUBLE, PLDOUBLE};
struct sometype {
    enum valuetype vt;
    void *value;
}
struct sometype getValue(...);

Upvotes: 3

yorodm
yorodm

Reputation: 4461

I'm afraid there's no way you can tell exactly what's stored in a (void *) pointer. If you want my advise, change your code and use proper typed pointers

Upvotes: 2

Related Questions