繁体   English   中英

C 中的“需要左值作为增量操作数”错误

[英]'Lvalue required as increment operand' error in C

这是我的代码片段:

void readandprint(){
    int* num = (int*) malloc (10* sizeof(int));
    for (int i =0;i<10;i++){
        *(num+i) = 0;
    }
    char c;
    while (scanf("%c",&c)==1){
        if (c>='0'&&c<='9'){
            *(num+c-'0')++ ; //error here
        }
    }
    for(int j = 0;j < 10;j++){
        printf("%d ",*(num+j));
    }
}

然后我得到了“需要作为增量操作数的左值”错误。 当我用“+=1”替换“++”时,代码工作得很好。 谁能告诉我为什么? 非常感谢您的任何建议。

如此 处所写,增量运算符比间接运算符具有更高的优先级。 所以,你的表达式被评估为 *((num+c-'0')++),其中子表达式 (num+c-'0') 不是左值,不能出现在增量的左侧操作员。

如果你想增加引用的 int 那么你需要:

 (*(num+c-'0'))++

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM