Inconsistent lvalue required error when incrementing array variable

后端 未结 3 1266
广开言路
广开言路 2021-01-19 19:54

I am getting \"lvalue required as increment operand\" while doing *++a. Where I am going wrong? I thought it will be equivalent to *(a+1). This beh

3条回答
  •  花落未央
    2021-01-19 20:26

    ++a and a + 1 are not equivalent. ++a is the same as a = a + 1, i.e it attempts to modify a and requires a to be a modifiable lvalue. Arrays are not modifiable lvalues, which is why you cannot do ++a with an array.

    argv declaration in main parameter list is not an array, which is why you can do ++argv. When you use a[] declaration in function parameter list, this is just syntactic sugar for pointer declaration. I.e.

    int main(int argc, char *argv[])
    

    is equivalent to

    int main(int argc, char **argv)
    

    But when you declare an array as a local variable (as in your question), it is indeed a true array, not a pointer. It cannot be "incremented".

提交回复
热议问题