2016-09-26 146 views
-1

我有一个结构:如何从指针中的指针引用变量中的值?

struct room; 
... 
/*some more structs */ 

typedef struct {  
    int state; 
    int id; 
    struct room* north; 
    struct room* south; 
    struct room* east; 
    struct room* west; 
    creature* creatures[10]; 
} room; 

而且,打印内容的功能:

void printContentsOfRoom(room* r) { 
    printf("\nRoom %d:\n", r->id); 
    printf("\tState: %s\n", getState(r)); 
    printf("\n\tNeighbors:\n"); 
    if (r->north.id != -1) 
    printf("\t North: Room %d\n",((room*)r->north)->id); 
    if (r->east.id != -1) 
    printf("\t East: Room %d\n",((room*)r->east)->id); 
    if (r->south.id != -1) 
    printf("\t South: Room %d\n",((room*)r->south)->id); 
    if (r->west.id != -1) 
    printf("\t West: Room %d\n",((room*)r->west)->id); 
    printf("\n\tCreatures:\n"); 
    for (int i = 0; i < 10; i++) { 
    creature* c = (creature*)r->creatures[i]; 
    if (c) { 
     if (c == pc) 
    printf("\t %s\n",getType(c)); 
     else 
    printf("\t %s %d\n",getType(c),c->id); 
    } 
    } 
} 

我试图让这个如果房间ID设置为-1(同时设置程序运行),它不会打印关于该房间的信息。在编译时,我得到错误“request for member'id'在一个不是结构或联合的东西。”

而且,我已经尝试设置如果条件R->北 - > ID,并返回错误“提领指向不完全型‘结构的房间’”

+0

'typedef struct {' - >'typedef struct room {'then'r-> north-> id' – BLUEPIXY

+0

如果您不明白问题,请不要使用转换! – Olaf

+0

'r-> west'是一个指针,对吧? '.'只适用于结构和联合,而指针不是结构或联合。所以'r-> west.anything'是无效的。但是你可以使用'(* r-> west).anything'或者'r-> west-> anything'。 – immibis

回答