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] [code] enum VarTypes { INTEGER = 1, FLOAT, DOUBLE, CHAR };
typedef struct { void* DataPtr; int SizeInBytes; int Type; // Values from VarTypes set } MYVAR;
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 }; [/code] [color=Blue]Now, you must pass var1...4 to every function to deal with variables. In short: BAD IDEA![/color]
Comments
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.
An approach that I thought was, to use memory addressing(&), but I am not able to put it to use to solve this.
[code]
enum VarTypes
{
INTEGER = 1,
FLOAT,
DOUBLE,
CHAR
};
typedef struct
{
void* DataPtr;
int SizeInBytes;
int Type; // Values from VarTypes set
}
MYVAR;
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 };
[/code]
[color=Blue]Now, you must pass var1...4 to every function to deal with variables. In short: BAD IDEA![/color]