LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to get the "data type" of an "unknown variable" in "C Language" ? (https://www.linuxquestions.org/questions/programming-9/how-to-get-the-data-type-of-an-unknown-variable-in-c-language-732693/)

Affair 06-13-2009 09:29 AM

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.

Sergei Steshenko 06-13-2009 10:00 AM

Quote:

Originally Posted by Affair (Post 3572710)
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.

No way. "C" does not pass type info at runtime.

bigearsbilly 06-13-2009 11:42 AM

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?

Affair 06-13-2009 09:26 PM

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.

graemef 06-13-2009 09:37 PM

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.

fantas 06-13-2009 09:49 PM

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
{
    void * linkeddata;
    uint32_t linkedtype;
};

TypeWithInfo GenerateType(uint32_t type)
{
    TypeWithInfo twi;

    switch (type)
    {
        case 0:
            twi.linkeddata = new SpecificType();
            twi.linkedtype = 0;
            break;

        // etc.
    }
    return twi;
}
void DestroyType(TypeWithInfo twi)
{
    switch (twi.linkedtype)
    {
        case 0:
            delete (SpecificType *) twi.linkeddata;
            break;

        // etc.
    }
}

Of course that's only really interesting for more complex types (allocated on the heap), as otherwise the data overhead would be too big.

(just noticed that while writing this reply that graemef at the same time had a remarkably similar idea)

graemef 06-13-2009 10:21 PM

Quote:

Originally Posted by fantas (Post 3573117)
(just noticed that while writing this reply that graemef at the same time had a remarkably similar idea)

But you reply came with the verbose switch ;)

kike_coello 06-18-2009 04:11 PM

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

ta0kira 06-20-2009 12:30 PM

Quote:

Originally Posted by kike_coello (Post 3578787)
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.

A good way to do this is to attempt parsing from more organized to less organized. For example:
Code:

unsigned int -> int -> double -> char*
This can be done with strtoul, etc. by checking the *endptr to make sure it's either 0x00 or whitespace. If all numerical parsing fails, just take it as a string.
Kevin Barry


All times are GMT -5. The time now is 10:01 AM.