By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,885 Members | 1,469 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,885 IT Pros & Developers. It's quick & easy.

printf pointer to double

P: n/a
Hello,

let us consider function

int foo(char *name, void *data) {
...
printf(name, data); /* Should be *data? */
...
}

and calling

double epsilon=1e-5;
foo("epsilon: %lg\n", &epsilon);

Function foo could change data, sopointer is needed.
Data could be of different type (int, double, long int),
but always is passed corresponding format (%...) for printf in `name'.

How to program this correct? How to correct dereference variable `data'?
gcc warns about derefencing void variable.
Or how to say it to printf, that it should dereference it's argument?

Thanks

Hans Ginzel
Nov 14 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Hans Ginzel <ha**@matfyz.cz> spoke thus:
int foo(char *name, void *data) { double *data) { /* easy */ ...
printf(name, data); /* Should be *data? */ Yes. ...
} double epsilon=1e-5;
foo("epsilon: %lg\n", &epsilon);


--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #2

P: n/a
On Wed, 5 May 2004, Hans Ginzel wrote:
Hello,

let us consider function

int foo(char *name, void *data) {
...
printf(name, data); /* Should be *data? */
...
}

and calling

double epsilon=1e-5;
foo("epsilon: %lg\n", &epsilon);

Function foo could change data, sopointer is needed.
Data could be of different type (int, double, long int),
but always is passed corresponding format (%...) for printf in `name'.

How to program this correct? How to correct dereference variable `data'?
gcc warns about derefencing void variable.
Or how to say it to printf, that it should dereference it's argument?
Will the second parameter always be a double? If yes, don't use void*. Use
double* instead.

If the second parameter can change based on the format string then you
are going to have to parse the format string, figure out what the data
type for the second parameter should be and cast it to the correct type
befroe you dereference it.
Thanks

Hans Ginzel


--
Send e-mail to: darrell at cs dot toronto dot edu
Don't send e-mail to vi************@whitehouse.gov
Nov 14 '05 #3

P: n/a
Hans Ginzel wrote:

Hello,

let us consider function

int foo(char *name, void *data) {
...
printf(name, data); /* Should be *data? */
...
}

and calling

double epsilon=1e-5;
foo("epsilon: %lg\n", &epsilon);
By the way, "%lg" and "%g" mean the same thing to
printf(); the "l" has no effect.
Function foo could change data, sopointer is needed.
Data could be of different type (int, double, long int),
but always is passed corresponding format (%...) for printf in `name'.

How to program this correct? How to correct dereference variable `data'?
gcc warns about derefencing void variable.
Or how to say it to printf, that it should dereference it's argument?


There is no easy way to do exactly what you ask.
You will need to parse the `name' string to find out
what data type is needed, and then convert `data' to
a pointer to that type before de-referencing it. In
pseudocode:

if (name needs a double)
printf (name, *(double*)data);
else if (name needs an int)
printf (name, *(int*)data);
else if (name needs a long int)
printf (name, *(long int*)data);
else ...

It might be better (and would certainly be easier)
to change the signature of the foo() function to be
more like that of printf() itself:

int foo_new(char *name, ...) {
va_list ap;
va_start (ap, name);
vprintf (name, ap);
va_end (ap);
...
}

Note that this change also requires a change in the way
you call the function:

double epsilon = 1e-10;
foo ("epsilon = %g\n", &epsilon);
foo_new ("epsilon = %g\n", epsilon);

(Observe that foo_new() takes the *value* of epsilon, not
a pointer to it.)

--
Er*********@sun.com
Nov 14 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.