简体   繁体   English

无法访问struct的struct数组中的成员变量

[英]can't access a member variable in struct array in struct

I am making a C program, which needs to access a struct array in a struct. 我正在制作一个C程序,该程序需要访问结构中的结构数组。

The definition looks like below 定义如下

struct def_world
{
    bool lock;
    char tilemap;
    def_tile tile[100][100];
struct def_tile
{
    bool lock;
    char kind;
    def_obj * obj;
    void * evt;
};
struct def_obj
{
    bool lock;
    int indexOfTable;
    bool frozen;
    char x,y;
    char kind;
    char face;
    char * msg;
    char * ip;
};

in the main function, I want to access world's tile[3][3] 's obj's face. 在主要功能中,我想访问世界的tile[3][3]的obj面孔。

I initialize world as 我将世界初始化为

def_world world={0,};

but the following lines make errors 但是以下几行出错

world.tile[3][3].obj=newobj();//newobj() returns def_obj type

world.tile[3][3].obj->face;

any idea how to access obj's face? 任何想法如何访问obj的脸吗?

Try these lines instead: 试试这些行:

 
 
 
  
  world.tile[3][3]->obj=newobj();//newobj() returns def_obj type world.tile[3][3]->obj.face;
 
  

Explanation: 说明:
world.tile[3][3] is a def_tile . world.tile[3][3]def_tile It's obj field isn't def_obj , but rather def_obj* . 它的 obj字段不是 def_obj ,而是 def_obj* Therefore, to get the def_obj that it points to, you should use ->obj . 因此,要获取它指向的 def_obj ,应使用 ->obj
Inside def_obj , face is just a char, so you would access it with .face . def_obj ,face只是一个字符,因此您可以使用 .face访问。

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

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