Thread: type cast void* parameter to be a ProductType pointer

  1. #1
    Registered User
    Join Date
    Nov 2020
    Posts
    2

    type cast void* parameter to be a ProductType pointer

    Code:
    typedef struct {
        EntityType ent;
        char name[MAX_STR];
        int  cost
    } ProductType;
    
    
    void moveProduct(void* product){
     //typecast?
    
    }
    I have a function moveProduct(void*) and I'm trying to figure out how to type cast the parameter to be a ProductType pointer, so that the product information is available. Does anyone know how to achieve this? I'm a little confused.

  2. #2
    Registered User
    Join Date
    Sep 2020
    Posts
    425
    You are playing with fire here... it will burn you!

    Here... have a match and some lighter fluid:

    Code:
      ProductType *p;
      p = (ProductType *)product.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Why don't you just change the parameter to be a ProductType pointer? If you cannot do this because say, this function is intended to be passed as some kind of generic callback, then yes, casting to ProductType* in the function body is the right approach.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. test void pointer before cast (instanceof)
    By jeanluca in forum C Programming
    Replies: 7
    Last Post: 05-29-2009, 04:26 AM
  2. Replies: 4
    Last Post: 08-27-2007, 11:51 PM
  3. Error: explicit type cast cannot convert 'void* to 'int*'
    By juschillin in forum C++ Programming
    Replies: 5
    Last Post: 09-04-2002, 09:19 AM
  4. type cast a pointer to void
    By rc7j in forum C++ Programming
    Replies: 1
    Last Post: 02-13-2002, 12:03 PM
  5. type cast to a pointer....
    By rc7j in forum C++ Programming
    Replies: 2
    Last Post: 02-05-2002, 04:13 PM

Tags for this Thread