473,729 Members | 2,243 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

from void * to types

Hello:

I have a simple program with a function that takes a void * parameter.
I want to be able to distinguish a type once the flow of control is
inside the body of the funtion. I am using conditional right now.
Unfortuneately the conditionals do not adequately filter the the types
that do not apply to the specific call. I could send a flag as a
second parameter, but I would rather the program dynamically recognize
the correct type. Here is the program, and output
as it sits:

// output
//the int is 10
//the double is -1.998009
//the int is 858993459
//the double is 12.600000
// note: the first double and the second int should not have made it
// note cont: past the given conditional.

// program
void process(void *x);
main() {

int i = 10;
double d = 12.6;
process(&i);
process(&d);
}
void process(void *x) {
if((int *)x) {
printf("the int is %d \n", *((int *)x) );
}
if((double *)x) {
printf("the double is %f\n", *((double *)x) );
}
}
Nov 14 '05 #1
3 1295
john wrote:
Hello:

I have a simple program with a function that takes a void * parameter.
I want to be able to distinguish a type once the flow of control is
inside the body of the funtion. I am using conditional right now.
Unfortuneately the conditionals do not adequately filter the the types
that do not apply to the specific call. I could send a flag as a
second parameter, but I would rather the program dynamically recognize
the correct type. [...]
Then you need a language other than C.

C is a statically-typed language, meaning that the type
of every expression must be known at compile time. There is
no construct in C that lets you ask "What is the type of
this expression?" or "Is this expression's type compatible
with type T?" because the expression's type is immutable
and the questions would be pointless.
void process(void *x) {
if((int *)x) {


You are apparently trying to ask "Is `x' a pointer to
`int'?" but this is not a question you can ask in C. What
you are actually asking is "When the value of `x' is converted
from type `void*' to type `int*', is the converted value unequal
to NULL?"

--
Er*********@sun .com

Nov 14 '05 #2
"john" <jp****@mail.us ask.ca> wrote in message
news:2b******** *************** ***@posting.goo gle.com...
Hello:

I have a simple program with a function that takes a void * parameter.
I want to be able to distinguish a type once the flow of control is
inside the body of the funtion. I am using conditional right now.
Unfortuneately the conditionals do not adequately filter the the types
that do not apply to the specific call. I could send a flag as a
second parameter, but I would rather the program dynamically recognize
the correct type.


The portable solution is to make your function variadic, send a "type" flag
as the first parameter, and the actual parameter as the second argument.

printf() is a perfect example of how to do this.

S

--
Stephen Sprunk "Stupid people surround themselves with smart
CCIE #3723 people. Smart people surround themselves with
K5SSS smart people who disagree with them." --Aaron Sorkin

Nov 14 '05 #3
Groovy hepcat Stephen Sprunk was jivin' on Fri, 04 Jun 2004 18:31:10
GMT in comp.lang.c.
Re: from void * to types's a cool scene! Dig it!
"john" <jp****@mail.us ask.ca> wrote in message
news:2b******* *************** ****@posting.go ogle.com...
Hello:

I have a simple program with a function that takes a void * parameter.
I want to be able to distinguish a type once the flow of control is
inside the body of the funtion. I am using conditional right now.
Unfortuneately the conditionals do not adequately filter the the types
that do not apply to the specific call. I could send a flag as a
second parameter, but I would rather the program dynamically recognize
the correct type.


The portable solution is to make your function variadic, send a "type" flag
as the first parameter, and the actual parameter as the second argument.


It doesn't have to be variadic. Just give the function the extra
parameter. There's no need to go complicating things with variadic
argument processing. Something like this would suffice:

enum type {INT, DOUBLE};

void process(void *x, enum type t)
{
switch(t)
{
case INT:
{
int *foo = x;
printf("The int is %d\n", *foo);
break;
}
case DOUBLE:
{
double *foo = x;
printf("The double is %f\n", *foo);
break;
}
default:
printf("Unknown type!\n");
break;
}
}

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technicall y correct" English; but since when was rock & roll "technicall y correct"?
Nov 14 '05 #4

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

Similar topics

9
4646
by: Anon Email | last post by:
Hi people, I'm learning about header files in C++. The following is code from Bartosz Milewski: // Code const int maxStack = 16; class IStack
2
6867
by: Alex Sedow | last post by:
Why explicit conversion from SomeType* to IntPtr is not ambiguous (according to standart)? Example: // System.IntPtr class IntPtr { public static explicit System.IntPtr (int); public static explicit System.IntPtr (long);
3
1851
by: Adam Nielsen | last post by:
Hi everyone, Yet another syntax problem that's baffling me with templates. I want to instantiate a template with a single parameter as per normal, however the parameter is actually a template class itself, with all *its* parameters filled out (in the form of a typedef.) I can't work out how to break apart the typedef to reveal what data types were used to create it in the first place. Here is some example code that demonstrates the...
28
1813
by: junky_fellow | last post by:
Guys, Consider a function func(void **var) { /* In function I need to typecast the variable var as (int **) I mean to say, I need to access var as (int **) }
35
1946
by: Lew Pitcher | last post by:
On November 14, 2008 15:00, in comp.lang.c, Nomen Nescio (nobody@dizum.com) wrote: Overkill Try
0
8913
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
8761
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9426
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...
1
6722
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
6016
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
4795
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3238
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
2
2677
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2162
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.