Thread: generic pointer to char conversion problem

  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
    4,632
    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 08:07 PM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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 08:02 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    Location
    Inside my computer
    Posts
    24,654
    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, 01:06 PM
  2. Replies: 9
    Last Post: 08-09-2011, 12:41 PM
  3. Problem with double conversion to char*
    By luhfluh in forum C Programming
    Replies: 3
    Last Post: 09-16-2010, 09: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, 01:57 AM