Thread: Passing float values as void* arguments

  1. #1
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    291

    Passing float values as void* arguments

    Hello, I have been trying to send different data types to a function that receives them as a void* argument, but I cannot get it run (only with int's and chars). That's what I'm trying to do

    Code:
    #include <stdio.h>
    
    void sample(short t,void *data)
    {
    switch(t)
        {
        case 0:
            {
            printf("INT=%d\n",(int*)data);
            }
        break;
        case 1:
            {
            printf("FLOAT=%f\n",(float*)data);
            }
        break;
        }
    }
    
    
    int main()
    {
    int ii;
    float ff;
    
    ii=27;
    sample(0,(int*)ii);
    
    ff=0.24f;
    sample(1,&ff);
    
    getchar();
    return 0;
    }
    The 'sample' function gets a definition of the type (0->int, 1->float), and a void* value. The first call it works well, but on the second call (with the float value) the output is 0.00000

    It is possible to do that?

    Thank's in advance.
    Niara

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    The fact that the first one works is pretty amazing, considering you're printing something totally different than what you think you are. Try these lines instead:
    Code:
    printf("INT=&#37;d\n", *(int*)data);
    ...
    printf("FLOAT=%f\n", *(float*)data);
    You were casting data to the right type, but failing to dereference it to get the value stored there.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    291
    Hello itsme86, thank's for your help and time.

    That works excellent! Now I can go on with my program.

    More thank's.
    Niara

  4. #4
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Also you probably want to be passing that int there with the & not casting it to a int* and passing it.
    Woop?

  5. #5
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    291
    Hey prog-bman, thank's for your time, and sorry for the delay on response.

    Yes, I already have seen that I should pass with the '&' instead casting it because the program crashes. That's how I have understanded that I should use it

    Code:
    #include <stdio.h>
    
    #define DRINT(p) (*(int*)p)
    #define DRFLT(p) (*(float*)p)
    
    void sample(short t,void *data)
    {
    switch(t)
        {
        case 0: {printf("INT=&#37;d\n",DRINT(data));} break;
        case 1: {printf("FLOAT=%f\n",DRFLT(data));} break;
        }
    }
    
    
    int main()
    {
    int ii;
    float ff;
    
    ii=27;
    sample(0,&ii);
    
    ff=0.24f;
    sample(1,&ff);
    
    getchar();
    return 0;
    }
    I suppose that the macros are right.
    And hope that can help someone else

    Niara

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    If you really wanted to use a macro, you could use a type-independent one like
    Code:
    #define void_deref(type, pointer) (*((type) *)(pointer))
    which would be used as
    Code:
    void_deref(int, data);
    void_deref(float, data);
    Personally I don't find the syntax confusing enough to warrant a macro, but there's nothing wrong with it.

    Also, an enum is a good candidate for the variable that indicates the type of the value pointed to by the void pointer.
    Code:
    enum type_t {
        TYPE_INT,
        TYPE_FLOAT
    };
    
    void sample(enum type_t type, void *data) {
        switch(type) {
        case TYPE_INT:
            printf("INT=&#37;d\n", *(int *)data);
            break;
        /* ... */
        }
    }
    
    int i = 234;
    sample(TYPE_INT, &i);
    Just a thought.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    291
    Hello dwks, thank's for your time and the type-independent macro.

    Ok, I also don't find the syntax very confusing, but I like to use to clarify a little the code.
    For the use of an enum, I think that I won't use it, maybe in future implementations of my program

    Thank's
    Niara

  8. #8
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Ok, I also don't find the syntax very confusing, but I like to use to clarify a little the code.
    For the use of an enum, I think that I won't use it, maybe in future implementations of my program
    Why not? It makes it easier to read and understand - and you can still use your old way, ie.
    Code:
    typedef enum type_t {
        TYPE_INT=0,
        TYPE_FLOAT
    } type;
    
    void sample(type t, void *data)
    {
         switch(t)
         {
              /* ... */
    You're free to call sample(0, blah) (for an int) or sample(TYPE_INT, blah);
    Last edited by zacs7; 07-12-2007 at 07:04 PM.

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    In C only, of course. In C++ you'd have to cast 0 to (type). But that's irrelevant, because if you ever needed to compile that code as C++ you could use an overloaded function like this:
    Code:
    void sample(int t, void *data) {
        sample((type)t, data);
    }
    Or one of many other solutions.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    291
    Hi zacs7, thank's for your apport and your time. I really cannot answer your question ("Why not?"), the program is still in a very initial phase, and don't discard to use enums.

    dwks: I suppose that I'll use in C, but I'll rememeber that.

    Thank's
    Niara

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Float Function Issues
    By GCNDoug in forum C++ Programming
    Replies: 5
    Last Post: 10-29-2007, 03:25 PM
  2. Need help with program
    By HAssan in forum C Programming
    Replies: 8
    Last Post: 06-10-2007, 08:05 PM
  3. Class won't call
    By Aalmaron in forum C++ Programming
    Replies: 3
    Last Post: 04-13-2006, 04:57 PM
  4. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 02:19 AM
  5. error declaration terminated incorrectly help
    By belfour in forum C++ Programming
    Replies: 7
    Last Post: 11-25-2002, 09:07 PM