简体   繁体   中英

Cannot seem to cast void* to struct in c++

I have a very simple method with the following prototype:

void *station_0(void* p1);

I am calling it like this:

product_record r;
pthread_create(thread, NULL, station_0, (void *)&r);

Inside this method all I need to do is cast p1 to an already defined struct product_record , I am currently trying this inside of my method:

product_record p = (product_record)p1

but the compiler complains on that line(above) saying error: invalid conversion from 'void*' to 'int' [-fpermissive]

I don't think I understand this warning at all. Why cannot I simply cast a void* to my struct?

You need two steps - convert the void pointer to a product_record pointer and then de-reference that. This can be done in the line

product_record p = *(static_cast<product_record *>(p1));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM