C 结构错误内的指向取消引用(间接寻址需要指针操作数)

C 结构错误内的指向取消引用(间接寻址需要指针操作数),c,C,下面的代码将current2移动到离我要停止的位置太远的一个节点: typedef struct s_coo { int x; int y; int z; void *next; } t_coo; typedef struct s_env { void *mlx; void *win; t_coo **head; } t_env; i

下面的代码将current2移动到离我要停止的位置太远的一个节点:

typedef struct  s_coo
{
    int     x;
    int     y;
    int     z;
    void    *next;
}              t_coo;

typedef struct  s_env
{
    void    *mlx;
    void    *win;
    t_coo   **head;
}               t_env;

int draw_map_y(t_env *e)
{
    t_coo   *current;
    t_coo   *current2;

    current = *(e->head);
    current2 = (*(e->head))->next;

    while (current2->y == 0)
        current2 = current2->next;
    return (0);
}
所以我试着在while循环中写:

while ((*(*current2))->next->y == 0)
而不是:

while (current2->y == 0)
但是我得到了错误“间接寻址需要指针操作数”。谁能给我解释一下,告诉我怎么写才对?我对C很陌生,谢谢

while ((*(*current2))->next->y == 0)
这是不正确的。正如错误“间接寻址需要指针操作数”所说,您可以将->应用于指针,但您是在(*(*current2))上进行的,这是错误的构造(
*current2
是类型为
struct s_coo
的对象,但该结构对象上的第二个
*
应该做什么?)

解决方案:

while (((t_coo *)current2->next)->y == 0)
((t_coo*)current2->next)->y的意思是

  • 取无效指针
    current2->next
  • 并将其视为指向
    t_-coo
    (即
    struct s_-coo
    上的typedef)的指针
  • 然后访问该浇铸指针上的
    y
    成员
  • 这是不正确的。正如错误“间接寻址需要指针操作数”所说,您可以将->应用于指针,但您是在(*(*current2))上进行的,这是错误的构造(
    *current2
    是类型为
    struct s_coo
    的对象,但该结构对象上的第二个
    *
    应该做什么?)

    解决方案:

    while (((t_coo *)current2->next)->y == 0)
    
    ((t_coo*)current2->next)->y的意思是

  • 取无效指针
    current2->next
  • 并将其视为指向
    t_-coo
    (即
    struct s_-coo
    上的typedef)的指针
  • 然后访问该浇铸指针上的
    y
    成员
  • 出现“间接寻址需要指针操作数”的错误是因为正在反引用指针。 下一个指针的类型也是void*。您需要将其类型转换为已知的指针类型。 这应该行得通

    while(((t_coo*)(current2->next))->y == 0)
    
    出现“间接寻址需要指针操作数”的错误是因为正在反引用指针。 下一个指针的类型也是void*。您需要将其类型转换为已知的指针类型。 这应该行得通

    while(((t_coo*)(current2->next))->y == 0)
    
    当(current2->y==0)正常时,您还更改了什么?当(current2->y==0)正常时,您还更改了什么?