Can anyone sol ve this casting issue?

This is a discussion on Can anyone sol ve this casting issue? within the C Programming forums, part of the General Programming Boards category; We have a structure defined something like (it does a lot more than this, but this is all that's necessary ...

  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
    Katy, Texas
    Posts
    2,309
    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 ;
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  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
    Katy, Texas
    Posts
    2,309
    Thanks. Then my suggestion should work fine, and no casting will be required.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

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, 05:56 AM
  2. Casting
    By morvick in forum C++ Programming
    Replies: 2
    Last Post: 06-18-2007, 12:06 AM
  3. Want to convert -ve value to +ve
    By hYph3n in forum C Programming
    Replies: 2
    Last Post: 09-05-2006, 02:52 AM
  4. casting the system exstracted date into seperate ints
    By bazzano in forum C Programming
    Replies: 1
    Last Post: 08-30-2005, 01:17 AM
  5. question about casting pointers/other types also??
    By newbie02 in forum C++ Programming
    Replies: 3
    Last Post: 08-07-2003, 06:01 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21