简体   繁体   中英

C - array of pointers to struct, syntax

I've got a struct A and an array of pointers to instances of that struct
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 ?

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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