Thread: Void* 2 Double

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    30

    Void* 2 Double

    Hello everybody!

    I've got a function which has only one arg and it is a void*.
    Is it possible somehow to convert this to double or float?

    Somebody told me that every standard data type can be converted to void* and vice-versa. What do you think, is this true?

    Any suggestion would be appreciated.

    Thanks in advance!
    Best Regards,

    Bill

  2. #2
    Registered User quagsire's Avatar
    Join Date
    Jun 2002
    Posts
    60
    Code:
    double test(void *p)
    {
     double d = *(double*)p;
     return d;
    }
    
    int main()
    {
     double x = 1234;
     cout << test(&x);
     return 0;
    }

  3. #3
    Registered User
    Join Date
    Aug 2002
    Posts
    30
    Hi everyboy!

    Thanks for the help for everyone. Then I was wrong. By the way, I've got more question about this:

    As you say it works only with pointer types. Then for example I can convert arrays to void* since those are pointers as well (at least that's what my compiler says.).

    That's Ok, but once I've converted something to void*, how could I find out what was the orginial data type of that array?

    E.g:

    int SizeofArray(void** array) {

    ...

    This function counts the elements of any array

    return count;

    }

    I would like to write a function like this, but if the data type is unknown, then I can't decide how to handle void** array or what to do with it.

    Any ideas?

    Thanks ind advance (and thanks again for the help)!
    Best Regards,

    Bill

  4. #4
    Registered User
    Join Date
    Aug 2002
    Posts
    30

    Wink Getting the original type of data stored in a void** Help! -:)

    Hi guys!

    I would like to write a function which counts the number of elements of an array.

    The function has only one arg, a void**.

    My question is as follows:

    I can covert any pointer type (including arrays) to void*, but once the conversion is done, how do I find out what was the orginial data type?

    E.g:

    int SizeofArray(void** array) {

    ...

    This function counts the elements of any array

    return count;

    }

    That's it. If the data type is unknown, then the func. can't decide how to handle void** array or what to do with it. I mean, for example an array of strings or an array of ints are different, and this funtion must use many array types.

    Any ideas or suggestions?

    Thanks for any help!
    Best Regards,

    Bill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copying 2-d arrays
    By Holtzy in forum C++ Programming
    Replies: 11
    Last Post: 03-14-2008, 03:44 PM
  2. Conversion From C++ To C
    By dicon in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 02:54 PM
  3. need some help with last part of arrays
    By Lince in forum C Programming
    Replies: 3
    Last Post: 11-18-2006, 09:13 AM
  4. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  5. Unknown Math Issues.
    By Sir Andus in forum C++ Programming
    Replies: 1
    Last Post: 03-06-2006, 06:54 PM