访问函数中的结构元素

时间:2013-11-30 23:43:46

标签: c struct compiler-errors

当我编译下面的代码时,我遇到了错误

snippet.c:24:24: error: request for member ‘px’ in something not a structure or union
snippet.c:25:24: error: request for member ‘py’ in something not a structure or union
snippet.c:27:22: error: request for member ‘x’ in something not a structure or union
snippet.c:27:45: error: request for member ‘y’ in something not a structure or union
snippet.c:31:14: error: request for member ‘x’ in something not a structure or union
snippet.c:35:16: error: request for member ‘y’ in something not a structure or union
snippet.c:39:25: error: request for member ‘px’ in something not a structure or union
snippet.c:40:25: error: request for member ‘py’ in something not a structure or union

以下代码

struct list{
    int x;
int y;
int f;
int g;
int h;
int px;
int py;
};

void history(int j, struct list *path[], struct list *closelist[], struct list *start, int *fxele);
void history(int j, struct list *path[], struct list *closelist[], struct list *start, int *fxele){

int i, p;

path[0] = closelist[j];
p = 1;

struct list tempsq;

tempsq.x = (*closelist).px[j];
tempsq.y = (*closelist).py[j];

while(tempsq.x=!start.x && tempsq.y =! start.y){

for(i = 0; i <= *fxele; i++){

if(closelist.x[i] = tempsq.x){

    for(j = 0; j <= *fxele; j++){

        if(closelist.y[j] = tempsq.y){

            path[p] = closelist[j];
            p = p+1;
            tempsq.x = closelist.px[j];
            tempsq.y = closelist.py[j];

            }
        }   
    }
}
}

return;
}

该代码旨在追溯寻路系统的正方形的父项,但具体情况并不重要。我很好奇我怎么称呼'closelist'和'start'阻止我调用他们的结构元素。

2 个答案:

答案 0 :(得分:0)

这些陈述

tempsq.x = (*closelist).px[j];
tempsq.y = (*closelist).py[j];  

错了。
pxpy不是您结构中的数组。而是尝试这个

tempsq.x = closelist[j].px;
tempsq.y = closelist[j].py;

答案 1 :(得分:0)

让我们说“l”是一个结构类型“list”的数组,“b”是该结构中字段的名称:

struct list {
    int b;
};

struct list l[10];

如果输入:

l.b[0];

这是不正确的。要从数组中的第一个“list”结构中获取“b”,您应键入:

l[0].b;

由于您的功能“历史”的一个参数是:

struct list *closelist[]

这意味着您正在传递一个指向列表结构数组的指针,因此如果您需要来自第j个结构的px和py数据成员,则必须键入:

(*closelist)[j].px;
(*closelist)[j].py;
相关问题