【问题标题】:Pointing dereference inside a struct error (indirection requires pointer operand)在结构错误中指向解引用(间接需要指针操作数)
【发布时间】:2016-02-24 08:15:09
【问题描述】:

下面的代码将 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->y == 0) 没问题,你还改了什么?

标签: c


【解决方案1】:
while ((*(*current2))->next->y == 0)

不正确。正如错误“间接需要指针操作数”所说,您可以将 -> 应用于指针,但您在 (*(*current2)) 上执行此操作,这是错误的构造(*current2struct s_coo 类型的对象,但第二个应该是 @ 987654324@ 在那个结构对象上呢?)。

解决方案:

while (((t_coo *)current2->next)->y == 0)

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

  1. 取空指针current2->next
  2. 并将其视为指向t_coo 的指针(即struct s_coo 上的typedef)
  3. 然后访问该转换指针上的y 成员

【讨论】:

    【解决方案2】:

    您得到“间接需要指针操作数”的错误是因为您正在取消引用指针。 下一个指针也是 void* 类型。您需要将其类型转换为已知的指针类型。 这应该可行,

    while(((t_coo*)(current2->next))->y == 0)
    

    【讨论】:

      猜你喜欢
      • 2015-06-02
      • 2014-10-31
      • 1970-01-01
      • 2014-06-30
      • 2015-01-04
      • 2011-06-09
      • 2013-01-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多