Thread: (void *) (void*) (product) what is it?

  1. #1
    Registered User
    Join Date
    May 2020
    Posts
    3

    (void *) (void*) (product) what is it?

    (void *) (void*) (product) what is it?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,794
    It looks like a cast of product to void*, then another unnecessary cast to void* again.
    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

  3. #3
    Registered User
    Join Date
    May 2020
    Posts
    3

    pthread_create(&threads[threadNumber], &attr, dotProduct, (void *) (void*) (product))

    Quote Originally Posted by laserlight View Post
    It looks like a cast of product to void*, then another unnecessary cast to void* again.
    will add context


    Code:
    pthread_create(&threads[threadNumber], &attr, dotProduct, (void *)(void*) (product));

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,794
    Yeah, that's it. Remove one of the two (void*) as it has no net effect.
    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
    Mar 2020
    Posts
    12
    pthread_create() seems to take a function pointer to a void* func(void*) type of function. I might be wrong though, the function pointer syntax in C is quite confusing.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,794
    Then product should have been defined as such a function; no casting needed. If you do want to cast, use a typedef: but then there's the issue of undefined behaviour if you're not careful.

    EDIT:
    I can't say I have much experience with casting to function pointer types, and in fact I usually typedef when I do have to deal with function pointers at all, but a quick check shows that if indeed the intention was to cast product to such a function pointer type without benefit of a typedef, it should have been: (void*(*)(void*))product

    EDIT #2:
    Well, another quick check shows that pthread_create looks like this:
    Code:
    int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg);
    So, cronus, while you're right that pthread_create does take such a function pointer, in alex01's post #3 that would be dotProduct, not the expression in question.
    Last edited by laserlight; 05-15-2020 at 05:23 AM.
    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

  7. #7
    Registered User
    Join Date
    Mar 2020
    Posts
    12
    Oh right, hadn't noticed that that weird cast was for just the normal void* argument. Indeed in that case, the second (void*) makes no sense.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. error: invalid conversion from 'const void*' to 'void*'
    By Wahidin Wahid in forum C++ Programming
    Replies: 10
    Last Post: 04-17-2012, 02:17 AM
  2. error invalid conversion from ‘const void*’ to ‘void*’
    By Wahidin Wahid in forum C Programming
    Replies: 3
    Last Post: 03-27-2012, 08:18 PM
  3. difference between void and static void function
    By mahaju in forum C++ Programming
    Replies: 7
    Last Post: 12-27-2011, 04:02 AM
  4. Replies: 12
    Last Post: 03-27-2009, 02:36 PM
  5. Invalid conversion from 'const void*' to 'void*' error
    By prawntoast in forum C Programming
    Replies: 3
    Last Post: 05-01-2005, 10:30 AM

Tags for this Thread