C 结构指针内的引用结构指针
很抱歉,我和你这么相似,但我有一个大脑放屁的时刻,我正在寻找一个好的和正确的方法来做到这一点 我正在启动一个新的pthread来处理一些数据,这意味着我们只能传递一个指针,在这种情况下,因为我们想向线程传递一些东西,指针指向一个结构 该数据结构本身包含指向另一个数据结构的指针 那么,在这种设置下,填充和访问嵌套结构的正确方法是什么C 结构指针内的引用结构指针,c,pointers,struct,nested,C,Pointers,Struct,Nested,很抱歉,我和你这么相似,但我有一个大脑放屁的时刻,我正在寻找一个好的和正确的方法来做到这一点 我正在启动一个新的pthread来处理一些数据,这意味着我们只能传递一个指针,在这种情况下,因为我们想向线程传递一些东西,指针指向一个结构 该数据结构本身包含指向另一个数据结构的指针 那么,在这种设置下,填充和访问嵌套结构的正确方法是什么 struct things_struct { int a; int b; }; struct thread_params { int fla
struct things_struct
{
int a;
int b;
};
struct thread_params
{
int flag;
struct things_struct *things;
}
int main()
{
struct thread_params params;
struct things_struct the_things;
params.things = &the_things;
// Launch thread
pthread_create(&thrptr, NULL, PrintThings, (void *)params);
//...
}
// The thread
void *PrintThings(void *arg)
{
struct thread_params *params = (thread_params *)arg; // cast back to correct type
int local_a = params->things->a; // is this correct?
//...
}
要添加对另一个类似问题的引用(我总是在发布后找到它们),有一个类似的问题,答案也同样简单。是-您访问things\u struct中的成员“a”的方式是正确的 但是-从我的脑海中-你应该把param的地址传递给phtread_create(…)
对@是的,他们有。你是真的在回答我的问题,还是只是在测试你关于评论的理论?谢谢-我没有写得很透彻,因为它只是为了举例说明。
pthread_create(&thrptr, NULL, PrintThings, (void *)¶ms);