from void * to types

J

john

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) );
}
}
 
E

Eric Sosman

john said:
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?"
 
S

Stephen Sprunk

john said:
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
 
P

Peter Shaggy Haywood

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!
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 "technically correct" English; but since when was rock & roll "technically correct"?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,143
Messages
2,570,822
Members
47,368
Latest member
michaelsmithh

Latest Threads

Top