[英]What is the (void*) type? And how can I dereference it?
Suppose you got a struct like this below: 假设您有一个像下面这样的结构:
struct msgbuf {
char msgtext[MAX_LENGTH];
}
And you cast it to (void*) 然后将其转换为(void *)
struct msgbuf m;
m.msgtext = "Text";
(void*) m;
What is (void*) and what value does it contain relating to the struct? 什么是(void *)?与该结构有关的值是什么?
How can I get the value of msgtext of m after the cast? 强制转换后如何获取m的msgtext的值? (I'm assuming by dereferencing it) (我通过取消引用来假设)
EDIT: To elaborate without posting too much code I want to pass a struct to a POSIX thread I am creating. 编辑:详细说明而不发布太多的代码,我想将结构传递给我正在创建的POSIX线程。
th_ret = pthread_create( &thread1, NULL, method_called, (void*) m);
void *method_called( void *rsp)
{
//get text from *rsp, which should be m passed
}
You want to pass &m
to the pthread_create()
(eg pass the struct by reference). 您想将&m
传递给pthread_create()
(例如,通过引用传递该结构)。 &m
is the address of your struct, and an address is compatible with a void *
. &m
是您的结构的地址,并且地址与void *
兼容。
Although a struct can be passed by value if declared explicitly that way in the function declaration, it is risky and frowned upon. 尽管如果在函数声明中以这种方式明确声明结构,则可以按值传递该结构,但它存在风险且不赞成使用。 When sent through a void *
argument, structs must be passed by reference, for one obvious reason - a struct cannot generally be contained in a sizeof(void *)
amount memory. 当通过void *
参数发送结构时,由于一个明显的原因, 必须通过引用传递结构-结构通常不能包含在sizeof(void *)
数量的内存中。
pthread_create()
uses the void *
argument type to be generic as possible, eg to accommodate a variety of fixed-length types that can fit in a small amount of stack space, where larger amounts of data can be indirectly referenced by passing a pointer to them instead. pthread_create()
使用void *
参数类型是通用为可能的,例如,以适应各种定长类型的可以容纳在堆栈空间,其中,大量的数据可以通过将指针传递到被间接引用的少量他们代替。
Where the function argument is a void *
, and where struct is defined: 函数参数为void *
,而struct定义为:
struct {
.
.
.
} myStruct;
This is legal: 这是合法的:
(void *)&myStruct // pass by reference
This is illegal (compiler disallows it): 这是非法的(编译器不允许这样做):
(void *)myStruct // pass by value
Sometimes people pass a simple int
or unsigned int
to their thread, because it is simpler, and sufficient for their need, but, commonly, a pointer to a struct
is passed by reference to convey an arbitrary amount of contextually relevant data. 有时人们将简单的int
或unsigned int
传递给线程,因为它更简单且足以满足他们的需要,但是通常,通过引用传递指向struct
的指针以传达任意数量的上下文相关数据。
An int
(and several other data types) can be cast into a void *
and vice versa, for example, if you just want to pass an integer, or float. 例如,如果您只想传递整数或浮点数,则可以将int
(和其他几种数据类型)转换为void *
,反之亦然。 What's important is that how you pass the argument is in agreement with how you interpret it later. 重要的是,您传递参数的方式应与以后的解释相一致。
Calling: 致电:
struct msgbuf {
char msgtext[MAX_LENGTH];
} m;
th_ret = pthread_create( &thread1, NULL, method_called, &m);
Handling: 处理:
void *method_called(void *rsp) {
struct msgbuf *m = rsp;
printf("%s\n", m->msgtext);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.