简体   繁体   English

如何从2个结构指针访问元素?

[英]How can i access an element from 2 structures pointers ?

I have a pointer & 2 different structures. 我有一个指针和2种不同的结构。 The first structure has a member that is a void* pointer. 第一个结构具有一个成员,该成员是void *指针。 Now i need to access a member of the second structure using the previous pointer to the first structure. 现在,我需要使用指向第一个结构的先前指针来访问第二个结构的成员。

struct a {
  void *ptrxx;
}

struct b {
  int info;
}

struct a *ptr;

I need to do something like : 我需要做类似的事情:

 ptr->ptrxx->info;

But i have to do some kind of typecasting to let C know that ptr->ptrxx is a pointer to struct b. 但是我必须进行某种类型转换以让C知道ptr->ptrxx是指向结构b的指针。 How can i do this in one expression (without the need of an extra pointer) ? 如何在一个表达式中执行此操作(不需要额外的指针)?

PS:I'm getting error: request for member ***** in something not a structure or union. PS:我遇到了error: request for member ***** in something not a structure or union. (gcc). (gcc)。

((struct b *)(ptr->ptrxx))->info

Probably don't need all those parens, but it helps to show the order of what's happenning. 可能不需要所有这些要素,但有助于显示正在发生的事情的顺序。 Take the ptrxx member of struct a (which is a void * ), cast it into a pointer to struct b , then take the info member of that. 接受struct aptrxx成员(它是一个void * ),将其转换为指向struct b的指针,然后获取该成员的info成员。

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

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