How to get the "data type" of an "unknown variable" in "C Language" ?
Hi guys,
I am going to write a function which will do different operation base on the data type of the input parameter. I am using C (Pure C, not C++). The problem is that I do not know how to get the data type of the input parameter in C Language. Can anyone tell me how to do this. |
Quote:
|
you cannot do it.
you can use variable arguments like printf does, var_args but what you suggest sounds silly. if they are different types what sort of function is it? a function should be a simple, cohesive operation operating on a strict range of data. what are you trying to do? |
Thank you to all of you replying me. I think you two answered my question.
To "bigearsbilly", I am trying to implement "generic programming" in C. Although it may be easier to implement in other languages, I just want to know if it can be done in C. |
I'm more of a C++ that C programmer but maybe you can mange it with void pointers or with a structure that is a union of the different types that you want to manage and (of course) an indicator of the actual type being passed in.
|
You could basically reinvent the wheel that C++ already has as a language feature. A basic approach could be (mixture C/C++ pseudo code):
Code:
struct TypeWithInfo (just noticed that while writing this reply that graemef at the same time had a remarkably similar idea) |
Quote:
|
i don't think i understand your problem very well, you say that the program is gonna ask for input and then you want to be able to recognize weather the input is an integer, float, character, string, etc?
in that case you can take the input into a character string and check each character, if it finds that all characters are numbers, then you can assume its an integer, if it finds a period then its a float/double and if it finds a combination then its a string. again, not sure i understood your problem. hope that helps. Enrique |
Quote:
Code:
unsigned int -> int -> double -> char* Kevin Barry |
All times are GMT -5. The time now is 10:01 AM. |