简体   繁体   English

似乎无法在C ++中将void *转换为struct

[英]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: 在此方法中,我所需要做的就是将p1转换为已定义的struct product_record ,我目前正在我的方法中尝试此操作:

product_record p = (product_record)p1

but the compiler complains on that line(above) saying error: invalid conversion from 'void*' to 'int' [-fpermissive] 但是编译器在该行(上面)抱怨说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? 为什么我不能简单地对我的结构施以void*

You need two steps - convert the void pointer to a product_record pointer and then de-reference that. 您需要两个步骤-将void指针转换为product_record指针,然后取消引用该指针。 This can be done in the line 这可以在线完成

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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