开发者

The difference between "&(epage->incoming[0]) = spage;" and "&(epage->incoming[p2]) = spage;"

开发者 https://www.devze.com 2023-03-05 09:09 出处:网络
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 lin

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 t开发者_JAVA百科he 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...


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?

0

精彩评论

暂无评论...
验证码 换一张
取 消