error: lvalue required as unary ‘&’ operand错误解决

    xiaoxiao2024-12-26  63

    #include <stdio.h> #include <stdlib.h> #include <string.h> #include <fcntl.h> #include <sys/time.h> #include <argz.h> int main(void) { size_t len; char *buff1 = malloc(100); char *buff2 = "zhangtao"; memset(buff1,'s',10); argz_add(&buff1,&(strlen(buff1)),buff2); printf("%s %d %s \n",__func__,__LINE__,buff1); free(buff1); return 0; }

    编译结果如下所示

    错误原因:

    因为函数的返回值存储在eax寄存器中,所以不可以直接操作,需要将其mov到内存中才可以操作。所有的函数都一样,不可以直接取地址。

    修改如下所示:

    #include <stdio.h> #include <stdlib.h> #include <string.h> #include <fcntl.h> #include <sys/time.h> #include <argz.h> int main(void) { size_t len; char *buff1 = malloc(100); char *buff2 = "zhangtao"; memset(buff1,'s',10); len = strlen(buff1); argz_add(&buff1,&(len),buff2); printf("%s %d %s \n",__func__,__LINE__,buff1); free(buff1); return 0; }

     

    最新回复(0)