Determining the data type of a variable using pointers in C

How do you determine the data type of a variable using only pointers?
I mean, if you have 3 variables say:

int x
float y
char z

How do you determine the data type of these variables by accessing them using pointers?


  • Pointers have types associated with them:
    int* px;
    float* py;
    char* pz;

    If it's a void pointer, then it's not known which type it points to:
    void* pv;

    It's possible to analyse the variables by accessing them, but the methods you would use to do this depend on the specific problem.
  • I got your point, but the question is, if I have 3 variables x, y and z, then how do I determine what data type they are of using pointers(no built-in functions)? Again, I assume I do not know what data type they are. How do I go about solving this?

    An approach that I thought was, to use memory addressing(&), but I am not able to put it to use to solve this.
  • [color=Blue]Just with a void* not possible. Only possible, if you create the structure for each variable and enclose the type inside it. But that would be completely different programming. Lots of overhead and unreadable.[/color]
    enum VarTypes
    INTEGER = 1,

    typedef struct
    void* DataPtr;
    int SizeInBytes;
    int Type; // Values from VarTypes set

    int a = 9;
    float b = 763.34f;
    double c = 6327.9988737;
    char s = 'A';

    MYVAR var1 = { &a, sizeof (a), INTEGER };
    MYVAR var2 = { &b, sizeof (b), FLOAT };
    MYVAR var3 = { &c, sizeof (c), DOUBLE };
    MYVAR var4 = { &s, sizeof (s), CHAR };
    [color=Blue]Now, you must pass var1...4 to every function to deal with variables. In short: BAD IDEA![/color]
  • Thanks for the info. It was helpful.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!