473,385 Members | 1,907 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,385 software developers and data experts.

printf pointer to double

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
3 3202
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Peter Ammon | last post by:
It's my understanding that the printf() function is declared as int printf(const char * restrict format, ...); in stdio.h. And loosely speaking, if a parameter is declared as restricted, then...
4
by: sushant | last post by:
hi why do we use '&' operator in scanf like scanf("%d", &x); but why not in printf() like printf("%d" , x); thnx in advance sushant
188
by: infobahn | last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
33
by: Kenneth Brody | last post by:
I know that passing printf() too few arguments, or arguments of the wrong type invokes UB. However, what about passing too many arguments, if the expected arguments are of the correct type? For...
15
by: Michael B Allen | last post by:
I'm printing tables of structure members in a terminal and I would like to use printf with an arbitrary pointer so that the code is smaller and I don't need to switch over ever possible type. ...
9
by: geek | last post by:
Hi all, Why does a printf("%d") statement prints this as output and when I do printf("%c") it doesn't print anything. -13361016 Any help would be appreciated. Thanks,
69
by: fieldfallow | last post by:
Hello all, Before stating my question, I should mention that I'm fairly new to C. Now, I attempted a small demo that prints out the values of C's numeric types, both uninitialised and after...
17
by: arindam.mukerjee | last post by:
I was running code like: #include <stdio.h> int main() { printf("%f\n", 9/5); return 0; }
18
by: Shane | last post by:
Hi all, I am learning C, and I have a problem with printf. I have a pointer that points to binary data, and I want to print that data out. If I cast the pointer to (char *) then the data is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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,...
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...

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.