*(a++) is giving error but not *(a+1)?? where a is array name?

前端 未结 3 1754
隐瞒了意图╮
隐瞒了意图╮ 2021-01-12 10:51

In following code:

void main()
{
    char a[]={1,5,3,4,5,6};
    printf(\"%d\\n\",*(a++)); //line gives error: wrong type argument to increment
    printf(\"         


        
3条回答
  •  无人共我
    2021-01-12 11:01

    'a' behaves like a const pointer. 'a' cannot change it's value or the address it is pointing to. This is because compiler has statically allocated memory of size of array and you are changing the address that it is referencing to.

    You can do it as follows instead

    void main()
    {
    char a[]={1,5,3,4,5,6};
    char *ch;
    ch=a;
    printf("%d\n",*(ch++)); //this lines gives error no more
    printf("%d\n",*(ch+1));
    }
    

提交回复
热议问题