Thread: Can anyone sol ve this casting issue?

  1. #1
    System.out.println("");
    Join Date
    Jan 2005
    Posts
    84

    Can anyone sol ve this casting issue?

    We have a structure defined something like (it does a lot more than this, but this is all that's necessary for my problem):

    Code:
    typdef struct
    {
    
    some_data_type source;
    
    void* val;
    
    int default
    
    }blah_st;
    Now I populate an array of these structures as follows: first choose data from source and if for some reason it isn't valid, we default val to default. Now the problem I have is that sometimes val isn't an int - sometimes it's a float. I have been solving this by putting in the IEEE floating point representation of the floating point default (ie, 0x428C0000 for 70.0) but we know it's wrong to using the underlying floating point representation this way. We would like to do something and be able to type in 70.0 and let the compiler figure it out.

    Now we have a solution for this that involves fairly significant rework. Can you see any way to do this without using things like void* to default, etc that would cause me to have to rework this data structure?

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    What you are saying about field "val" makes absolutely no sense to me, because you have field "val" defined as a "void *" but are talking about using it as an int. And, it's not clear what "we default val to default" means. Does this mean you put a default value of some sort in field "default", or, or you saying the field "default" gets its default value from field "val"?

    Regardless, why not use a union? Declare a union of either an int or a float, and then use
    Code:
    myunion * val ;
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    System.out.println("");
    Join Date
    Jan 2005
    Posts
    84
    Quote Originally Posted by Dino View Post
    What you are saying about field "val" makes absolutely no sense to me, because you have field "val" defined as a "void *" but are talking about using it as an int. And, it's not clear what "we default val to default" means. Does this mean you put a default value of some sort in field "default", or, or you saying the field "default" gets its default value from field "val"?

    Regardless, why not use a union? Declare a union of either an int or a float, and then use
    Code:
    myunion * val ;
    "val" is just a pointer to some variable that we either populate with "source" if it's valid or populate with "default" if "source" is not valid. The variable pointed to by "val" is sometimes a float and other times an int.

    Sorry for the confusion.

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Thanks. Then my suggestion should work fine, and no casting will be required.
    Mainframe assembler programmer by trade. C coder when I can.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. float calculation issue
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 05-26-2008, 04:56 AM
  2. Casting
    By morvick in forum C++ Programming
    Replies: 2
    Last Post: 06-17-2007, 11:06 PM
  3. Want to convert -ve value to +ve
    By hYph3n in forum C Programming
    Replies: 2
    Last Post: 09-05-2006, 01:52 AM
  4. casting the system exstracted date into seperate ints
    By bazzano in forum C Programming
    Replies: 1
    Last Post: 08-30-2005, 12:17 AM
  5. question about casting pointers/other types also??
    By newbie02 in forum C++ Programming
    Replies: 3
    Last Post: 08-07-2003, 05:01 AM