Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 将void*强制转换为作为指针的结构?_C_Pointers_Struct_Casting - Fatal编程技术网

C 将void*强制转换为作为指针的结构?

C 将void*强制转换为作为指针的结构?,c,pointers,struct,casting,C,Pointers,Struct,Casting,我有一个链表,其中每个节点的形式如下: struct queueItem { struct carcolor *color; int id; }; typedef struct queueItem *CustDetails; 我想运行以下函数: extern void mix(struct carcolor *v); 但是,该函数在以下范围内运行: void foo(void *v) //v should be the pointer to the dequeued q

我有一个链表,其中每个节点的形式如下:

struct queueItem
{
    struct carcolor *color;
    int id;
};
typedef struct queueItem *CustDetails;
我想运行以下函数:

extern void mix(struct carcolor *v);
但是,该函数在以下范围内运行:

void foo(void *v)    //v should be the pointer to the dequeued queueItem
{
    //do other stuff
    mix(v->color);
}
这会产生以下错误:

request for member ‘color’ in something not a structure or union
当函数原型为
void foo(void*v)
时,如何访问
struct carcolor*color


我尝试强制转换
(struct queueItem)v
,但没有成功。

您需要强制转换到指向该结构的指针

    mix(((struct queueItem *)v)->color);

在这些情况下,我喜欢做的是获取一个本地指针并使用它

void foo(void *v)    //v should be the pointer to the dequeued queueItem
{
    struct queueItem *localpointer = v;
    //do other stuff
    mix(localpointer->color);
}

您需要强制转换到指向该结构的指针

    mix(((struct queueItem *)v)->color);

在这些情况下,我喜欢做的是获取一个本地指针并使用它

void foo(void *v)    //v should be the pointer to the dequeued queueItem
{
    struct queueItem *localpointer = v;
    //do other stuff
    mix(localpointer->color);
}

强制转换为
((struct queueItem*)v)->color
。强制转换为
((struct queueItem*)v)->color
。您的意思是
struct queueItem
您在哪里编写的
struct carcolor
v
是指向
queueItem
的指针,它包含指向
struct carcolor
的指针。。。是的,我的意思是与
v
最初的类型相同。答案已编辑。我想你也忘了编辑第一行。一旦问题解决,我会将你的答案标记为正确。你是说你在哪里写的
struct-carcolor
v
是指向
queueItem
的指针,它包含指向
struct carcolor
的指针。。。是的,我的意思是与
v
最初的类型相同。答案已编辑。我想你也忘了编辑第一行。一旦问题解决了,我会把你的答案标记为正确的。