generic pointer to char conversion problem

This is a discussion on generic pointer to char conversion problem within the C++ Programming forums, part of the General Programming Boards category; compiler is complaining for this string hash function I found online: error: invalid conversion from 'void*' to 'unsigned char*' Code: ...

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    87

    generic pointer to char conversion problem

    compiler is complaining for this string hash function I found online:

    error: invalid conversion from 'void*' to 'unsigned char*'

    Code:
    //FNV string hash fcn (src:http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx)
    unsigned fnv_hash ( void *key, int len )//Q: how do I use generic ptr key?
    {
       unsigned char* p = key;
       unsigned h = 2166136261U;
       int i;
     
       for ( i = 0; i < len; i++ )
         h = ( h * 16777619 ) ^ p[i];
     
       return h;
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,895
    If you are using a C++ compiler you will probably need to cast your void* to the proper type.

    Jim

  3. #3
    Registered User
    Join Date
    Jul 2012
    Posts
    87
    It's fixed w/ casting as you mentioned:
    Code:
    unsigned char* p = (unsigned char*)key;
    BUT shouldnt' it just be
    Code:
    unsigned char* p = (unsigned char)key;
    which doesn't compile. I ask b/c in function parameters, we have declared key to be a void pointer, so if it's already a pointer, I'd think the (unsigned char*)key would be dereferencing a ptr...
    Last edited by monkey_c_monkey; 07-11-2012 at 09:07 PM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,119
    we have declared key to be a void pointer, so if it's already a pointer, I'd think the (unsigned char*)key would be dereferencing a ptr[/quote]
    The syntax that you're using is:
    Code:
    x = (type_of_x)y;
    Hence, there is no dereferencing. You are type casting. Therefore, you should be casting to unsigned char*, not unsigned char.

    EDIT:
    Then again, in this case consider using static_cast, e.g.,
    Code:
    unsigned char* p = static_cast<unsigned char*>(key);
    Last edited by laserlight; 07-11-2012 at 09:02 PM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jul 2012
    Posts
    87
    I have not learnt about static_cast, but for the type casting format, it's still confusing... But if that's how the compilers roll, I'll go w/ it... I hope you see where my logic is.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,900
    But, you know, a cast merely changes the type of something. It never dereferences something.
    Therefore, if you do
    unsigned char* p = (unsigned char)key;
    You are telling the compiler to take the pointer, and convert it (truncate it) to fit an unsigned char. Doesn't make sense.
    unsigned char* p = (unsigned char*)key;
    This on the other hand tells the compiler to change key into a unsigned char*, which makes sense because we want it to be a pointer to char.
    Re not having learned static_cast: google it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 06-18-2012, 02:06 PM
  2. Replies: 9
    Last Post: 08-09-2011, 01:41 PM
  3. Problem with double conversion to char*
    By luhfluh in forum C Programming
    Replies: 3
    Last Post: 09-16-2010, 10:42 AM
  4. Conversion Char To Char * Problem
    By ltanusaputra in forum Windows Programming
    Replies: 3
    Last Post: 03-01-2008, 02:06 PM
  5. Problem regarding binary to char conversion
    By LuP in forum C Programming
    Replies: 1
    Last Post: 04-19-2002, 02:57 AM

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