Name:
Anonymous
2008-03-19 17:18
How do I check what type of variable a variable is?
Something like;
if (typecheck(foo,int))
{
/* lol */
}
Name:
EXPERT PROGRAMMER
2008-03-19 23:49
Basically you need to create a list of all the available types and their sizes, and then use sizeof on your variable to determine which type it is.
char* types[4] = {
"char", // 1 byte
"short", // 2 bytes
"long", // 4 bytes
"long long," // 8 bytes
};
int sizes[4] = { 1, 2, 4, 8 };
bool typecheck (variable, type)
{
int i;
for (; i <= 3; i++)
{
if (strcmp(types[i], type)
return sizes[i] = sizeof(variable) ? TRUE : FALSE;
}
return FALSE;
}