printf pointer to double

Discussion in 'C Programming' started by Hans Ginzel, May 5, 2004.

  1. Hans Ginzel

    Hans Ginzel Guest

    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
     
    Hans Ginzel, May 5, 2004
    #1
    1. Advertising

  2. Hans Ginzel <> 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.
     
    Christopher Benson-Manica, May 5, 2004
    #2
    1. Advertising

  3. 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
     
    Darrell Grainger, May 5, 2004
    #3
  4. Hans Ginzel

    Eric Sosman Guest

    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.)

    --
     
    Eric Sosman, May 5, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sydex
    Replies:
    12
    Views:
    6,528
    Victor Bazarov
    Feb 17, 2005
  2. ben
    Replies:
    4
    Views:
    626
    Martin Ambuhl
    Jun 26, 2004
  3. whatluo

    (void) printf vs printf

    whatluo, May 26, 2005, in forum: C Programming
    Replies:
    29
    Views:
    1,256
  4. fieldfallow

    printf() error with long double and null pointer.

    fieldfallow, Feb 24, 2006, in forum: C Programming
    Replies:
    69
    Views:
    1,739
    Jordan Abel
    Feb 28, 2006
  5. azza

    printf affects following printf/s

    azza, Oct 17, 2010, in forum: C Programming
    Replies:
    0
    Views:
    435
Loading...

Share This Page