Hi guys
Is there a command in C that returns the type of a variable? E.g. if we declare
then is there a command X such that X(a) returns int (or any other variable type, for that matter)?Code:int a;
Best,
Niles.
Hi guys
Is there a command in C that returns the type of a variable? E.g. if we declare
then is there a command X such that X(a) returns int (or any other variable type, for that matter)?Code:int a;
Best,
Niles.
C is a statically typed language. Mostly what that means is that if something has a particular type, it's because your code explicitly made it that type. You can cast variables to other types, but one of the most powerful features of C is that it's then treated 100% like that type. Every thing is written directly as binary data to memory - there's no meta-data to remember what you intended something to be like. If you cast a char to an int, it's now an int. If you cast it to a void *, it's now generic. And if you're not casting, there's no way to not know what type something should be.
Depending on what it is you're trying to do, we might be able to make some suggestions as to go about something like this. For instance, if you have a function that take a void * but might treat it differently depending on what type the calling function intended it to be, you could also pass in an enum or something to specify the type, and have conditionals to handle that inside the function.