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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
06-13-2009, 09:29 AM
|
#1
|
LQ Newbie
Registered: Jun 2009
Posts: 2
Rep:
|
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.
|
|
|
06-13-2009, 10:00 AM
|
#2
|
Senior Member
Registered: May 2005
Posts: 4,481
|
Quote:
Originally Posted by Affair
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.
|
|
|
06-13-2009, 11:42 AM
|
#3
|
Senior Member
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,516
|
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?
|
|
|
06-13-2009, 09:26 PM
|
#4
|
LQ Newbie
Registered: Jun 2009
Posts: 2
Original Poster
Rep:
|
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.
|
|
|
06-13-2009, 09:37 PM
|
#5
|
Senior Member
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379
Rep: 
|
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.
|
|
|
06-13-2009, 09:49 PM
|
#6
|
Member
Registered: Jun 2007
Location: Bavaria
Distribution: slackware, xubuntu
Posts: 143
Rep:
|
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.
|
|
|
06-13-2009, 10:21 PM
|
#7
|
Senior Member
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379
Rep: 
|
Quote:
Originally Posted by fantas
(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 
|
|
|
06-18-2009, 04:11 PM
|
#8
|
Member
Registered: Jul 2005
Location: maryland
Distribution: Ubuntu 9.04
Posts: 88
Rep:
|
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
|
|
|
06-20-2009, 12:30 PM
|
#9
|
Senior Member
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078
Rep: 
|
Quote:
Originally Posted by kike_coello
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 08:27 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|