LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 06-13-2009, 09:29 AM   #1
Affair
LQ Newbie
 
Registered: Jun 2009
Posts: 2

Rep: Reputation: 0
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.
 
Old 06-13-2009, 10:00 AM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 451Reputation: 451Reputation: 451Reputation: 451Reputation: 451
Quote:
Originally Posted by Affair View Post
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.
 
Old 06-13-2009, 11:42 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,269

Rep: Reputation: 165Reputation: 165
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?
 
Old 06-13-2009, 09:26 PM   #4
Affair
LQ Newbie
 
Registered: Jun 2009
Posts: 2

Original Poster
Rep: Reputation: 0
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.
 
Old 06-13-2009, 09:37 PM   #5
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,376

Rep: Reputation: 147Reputation: 147
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.
 
Old 06-13-2009, 09:49 PM   #6
fantas
Member
 
Registered: Jun 2007
Location: Bavaria
Distribution: slackware, xubuntu
Posts: 143

Rep: Reputation: 22
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)

Last edited by fantas; 06-13-2009 at 09:51 PM.
 
Old 06-13-2009, 10:21 PM   #7
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,376

Rep: Reputation: 147Reputation: 147
Quote:
Originally Posted by fantas View Post
(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
 
Old 06-18-2009, 04:11 PM   #8
kike_coello
Member
 
Registered: Jul 2005
Location: maryland
Distribution: Ubuntu 9.04
Posts: 88

Rep: Reputation: 17
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
 
Old 06-20-2009, 12:30 PM   #9
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by kike_coello View Post
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Telling people to use "Google," to "RTFM," or "Use the search feature" Ausar General 77 03-21-2010 11:26 AM
net working eth0 eth1 wlan0 "no connection" "no LAN" "no wi-fi" Cayitano Linux - Newbie 5 12-09-2007 07:11 PM
Standard commands give "-bash: open: command not found" even in "su -" and "su root" mibo12 Linux - General 4 11-11-2007 10:18 PM
LXer: Displaying "MyComputer", "Trash", "Network Servers" Icons On A GNOME Desktop LXer Syndicated Linux News 0 04-02-2007 08:31 AM
<input type="button" disabled="true" > does not work in ns4.7 or 4.9 cybercop12us Programming 2 11-29-2002 08:31 AM


All times are GMT -5. The time now is 08:30 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration