“&(epage->incoming[0]) = spage;”之间的区别是和“&(epage->incoming[p2]) = spage;”

发布于 2024-11-07 03:47:04 字数 1201 浏览 1 评论 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;

其中spageepage是下面定义的结构体:

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返回一个指向structwebpage的指针。

通过将结构 outgoingincoming 更改为双指针并将最后一行更改为 (epage->incoming[p2]) = spage;< 解决了问题/代码>。

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

The last line of this particular code block produces the error "lvalue required as left operand of assignment". The confusion lies in why the last line throws this error while the second to last line doesn't.

      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;

where spage, epage are structs defined below:

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;
};

and the function find returns a pointer to a struct webpage.

Problem was resolved by changing the structs outgoing and incoming to double pointers and changing the last line to (epage->incoming[p2]) = spage;.

Still don't know why the error happened though...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

灯下孤影 2024-11-14 03:47:04

此代码:

struct S {
    int * p;
};

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

使用 GCC 4.5.1 编译,产生:

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

换句话说,两种情况都会产生所需左值错误。你能用你的编译器尝试一下这个确切的代码吗?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文