0

此特定代码块的最后一行产生错误“需要左值作为赋值的左操作数”。困惑在于为什么最后一行会引发此错误,而倒数第二行则不会。

      int p2 = 0;
      spage = find(in.startpage);
      spage->noutgoing++;
      spage->outgoing = (struct webpage *)realloc((spage->outgoing),((spage->noutgoing)*sizeof(struct webpage)));

      epage = find(in.endpage);
      epage->nincoming++;
      epage->incoming = (struct webpage *)realloc((epage->incoming),((epage->nincoming)*sizeof(struct webpage)));


      position = ((epage->nincoming));

      &(epage->incoming[0]) = spage;
      &(epage->incoming[p2]) = spage;

其中spage,epage是下面定义的结构:

struct webpage {
   char name;               /* name of page */
   struct webpage *outgoing;    //array of pointers pointing to outgoing webpages
   struct webpage *incoming ;   //array of pointers pointing to incoming webpages
   int noutgoing;
   int nincoming;
};

并且该函数find返回一个指向 a 的指针struct webpage

通过更改结构outgoingincoming双指针并将最后一行更改为(epage->incoming[p2]) = spage;.

仍然不知道为什么会发生错误...

4

1 回答 1

0

This code:

struct S {
    int * p;
};

int main() {
    struct S * s;
    int n;
    &(s->p[42]) = 0;
    &(s->p[n]) = 0;
}

compiled with GCC 4.5.1, produces:

n.c:9:16: error: lvalue required as left operand of assignment
n.c:10:15: error: lvalue required as left operand of assignment

In other words, both cases produce the lvalue needed error. Can you try this exact code with your compiler?

于 2011-05-13T08:18:54.533 回答