473,396 Members | 1,599 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 1269
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.usask.ca> wrote in message
news:2b**************************@posting.google.c om...
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.usask.ca> wrote in message
news:2b**************************@posting.google. 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 "technically correct" English; but since when was rock & roll "technically correct"?
Nov 14 '05 #4

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

Similar topics

9
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
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...
3
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...
28
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
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.