473,769 Members | 1,917 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Way to determine type of variable?

I would like to write a generic procedure that will take string or numeric
variables. I can not think of a way to make this more clear except to show
what I want.

int main(void)
{
int i=7;
char *s="/etc/filesystems";

generic(i);
generic(s);
generic("Hello" );
generic(2048);
exit(0);
}

should return:
7
/etc/filesystems
Hello
2048

so...

void generic( ???????)
{
???
}

I have looked at sprintf, varargs etc.

Could someone point me in the right direction?

Thanks,

Walter
Nov 14 '05
21 10672
Eric Sosman wrote:
Sam Dennis wrote:
Walter L. Preuninger II wrote:
<Je********* **@physik.fu-berlin.de> wrote:
void xerror( const char *fmt, ... )
{
/* Put printing argv[0], __FILE__ and __LINE__ in here */
[You can't.]


#define xerror( fmt, ... ) xerror( "%s:%d - " fmt, __FILE__, __LINE, \
__VA_ARGS__ )

However, this has several limitations (other than requiring C99 macros)
so I'm sure that you'll want to try to make something better.


#define XERROR xwhere(__FILE__ , __LINE__) , xerror


Almost precisely what I was thinking of, but I wanted to leave something
for the OP to do.

--
++acr@,ka"
Nov 14 '05 #21
In article <40************ **@sun.com>,
Eric Sosman <Er*********@Su n.COM> wrote:
....
The fundamental problem is that C is a statically-typed
language, meaning that the type of every expression is fixed
at compile time. True, <stdarg.h> (not <varargs.h>, BTW) can
evade this requirement, but only briefly: In order to access
the arguments corresponding to `...', you must use an expression
whose type is known and immutable. C is not Lisp.


Funny you should mention that - and in just that way.

<rant>
WARNING WARNING WARNING - OFF TOPIC OFF TOPIC OFF TOPIC (*)
</rant>

If you google to the right place, you'll find the following text posted by
one K. McCormack at some point in the not so distant past:

<text from google>
The answer is a neat little package called "ffcall", which came out of the
Common Lisp effort, and allows you to setup calls to variadic functions at
something like the assembler level - you basically are pushing args onto
the stack, in a loop. The ffcall package contains 4 related things - the
only one you need for my version of strfmon() is the "avcall" package.

You can get ffcall from: ftp://ftp.santafe.edu/pub/gnu/ffcall-1.6.tar.gz
</text from google>

Note that ffcall is described as "var args in reverse", but I think of it
as "var args at runtime". Note that var args is all compile-time.

The point is that *it* *allows* you to write a C program that works like
this:

:loop
Enter a type and a value...
goto :loop

After exiting loop (running the loop an arbitrary number of times), call
printf, and print out all the values with the right types. That's
something you can't do with var args.

(*) Although, as an aside, is it really OT (and thus an unforgivable sin)
to state that "X can't be done in Standard C, but here's a pointer
a library (or similar external facility) that allows you to do what you
want" ?

Nov 14 '05 #22

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
6149
by: John Bentley | last post by:
John Bentley: INTRO The phrase "decimal number" within a programming context is ambiguous. It could refer to the decimal datatype or the related but separate concept of a generic decimal number. "Decimal Number" sometimes serves to distinguish Base 10 numbers, eg "15", from Base 2 numbers, Eg "1111". At other times "Decimal Number" serves to differentiate a number from an integer. For the rest of this post I shall only use either...
3
2585
by: Shahid Juma | last post by:
Hello All, This may be a trivial question, but I was wondering how can you determine if a value contains an Integer or Float. I know there is a function called IsNumeric, however you can't determine what type it is. Thanks for the help, Shahid
4
3058
by: MCollins | last post by:
trying to determine a variable type, specifically that a variable is an integer. i tried using type(var) but that only seemed to produce a response in the command line. is there a built in python function to determine if a variable is an integer?
14
24434
by: J. Jones | last post by:
Suppose the following: class MyContainer : System.Collections.CollectionBase { //... } (where CollectionBase implements IList, ICollection) How do I determine if a type (such as MyContainer) derives from IList?
3
1171
by: M Shafaat | last post by:
Hi! I want to write C# code by which I can determine the name of an object and/or variable at run-time. VS design windows does it when you drag a control onto your form. It uses the name of the control's type and adds a number to it and creates a name for the object, like "button2". Regards M Shafaat
16
3425
by: Jm | last post by:
Hi All Is it possible to determine who is logged onto a machine from inside a service using code in vb.net ? I have found some code that seems to work under vb6, but doesnt under .NET ? Any help is greatly appreciated Thanks
1
1567
by: tshad | last post by:
I have some code to go through a session collection for my error page routine and I get an error on my objects that I store in session variables. Dim strName as String Dim iLoop as Integer For Each strName in Session.Contents trace.warn(strName & " - " & Session.Contents(strName)) Next
5
6843
by: MLH | last post by:
Suppose MyName, a string variable equals "frmEnterClients". How can I determine ... 1) if frmEnterClients exists as an object in the database? 2) what type of object is it (tbl, qry, frm, rpt, mac, mod)?
8
1704
by: Ole | last post by:
If I define a class and create a instant of it like e.g.: UserClass instantName = new UserClass(); how do I then determine the defined name "instantName" in the UserClass e.g. in a method (or a property) like this: public void userFunction() { string Var = new string();
29
5115
by: garyusenet | last post by:
I'm trying to investigate the maximum size of different variable types. I'm using INT as my starting variable for exploration. I know that the maximum number that the int variable can take is: 65,535. But i'm trying to write a program to test this, assuming I didn't know this number in advance. I came up with the following but have two questions. Maybe someone can help? using System; using System.Collections.Generic; using System.Text;
0
9579
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10205
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10035
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7401
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6662
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5293
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5441
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3949
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2811
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.