【问题标题】:can you explain why compile time error in the below code你能解释一下为什么在下面的代码中编译时错误
【发布时间】:2021-08-14 05:56:01
【问题描述】:

为什么我在下面给出的代码中遇到错误......

#include <stdio.h>
    void foo(int*);
    int main()
    {
        int i = 10;
        foo((&i)++);(in this line error shows like this) //error: lvalue required as increment operand
    }
    void foo(int *p)
    {
        printf("%d\n", *p);
    }

【问题讨论】:

  • 当询问错误时,请显示准确和完整的错误消息。

标签: c pointers function-pointers


【解决方案1】:

来自Member access operators

address-of 运算符产生其操作数的非左值地址,适用于初始化指向操作数类型的指针。

来自Increment/decrement operators

前缀和后缀递增或递减的操作数 expr 必须是整型(包括 _Bool 和枚举)、实浮点型或指针类型的可修改左值。

简单地说,&amp; 运算符不会为++ 运算符生成合适的对象。

【讨论】:

    猜你喜欢
    • 2021-12-18
    • 2021-10-27
    • 2011-04-20
    • 2016-02-09
    • 2018-04-14
    • 1970-01-01
    • 2011-04-19
    • 1970-01-01
    相关资源
    最近更新 更多