“&(epage->incoming[0]) = spage;”之间的区别是和“&(epage->incoming[p2]) = spage;”
该特定代码块的最后一行产生错误“需要左值作为赋值的左操作数”。令人困惑的是为什么最后一行抛出此错误,而倒数第二行却没有。
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
返回一个指向structwebpage
的指针。
通过将结构 outgoing
和 incoming
更改为双指针并将最后一行更改为 (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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
此代码:
使用 GCC 4.5.1 编译,产生:
换句话说,两种情况都会产生所需左值错误。你能用你的编译器尝试一下这个确切的代码吗?
This code:
compiled with GCC 4.5.1, produces:
In other words, both cases produce the lvalue needed error. Can you try this exact code with your compiler?