简体   繁体   English

C-指向结构,语法的指针的数组

[英]C - array of pointers to struct, syntax

I've got a struct A and an array of pointers to instances of that struct 我有一个结构A和一个指向该结构实例的指针的数组
I'm trying to access a member directly from the array but I don't know what's the right syntax to do it : 我正在尝试直接从数组访问成员,但是我不知道正确的语法是什么:

struct A  
{  
  int a;  
  void** b;  
}

A* p = (A*) malloc(sizeof(A));  
p->b = (A**) malloc(sizeof(A*) * 3);

//
// something is done
//

int c;

A* test = p->b[0];
c = test->a;

Basically what I'm asking is how do I get rid of the intermediate A* test so I can assign the value of c in one line ? 基本上,我要问的是如何摆脱中间的A *测试,以便可以在一行中分配c的值?

Just do 做就是了

int c = ((struct A*) (p->b[0]))->a;

Defining 定义

struct A  
{  
  int a;  
  struct A ** b;  
}

this would do 这会做

int c = p->b[0]->a;

你可以做 :

c = ((A*) (p->b[0]))->a;

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

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