Why can't I increment an array?

前端 未结 5 1179
野的像风
野的像风 2020-12-20 07:32
char a[] = \"hello\";

My understanding is that a acts like a constant pointer to a string. I know writing a++ won\'t work

5条回答
  •  一生所求
    2020-12-20 08:11

    I think this answer here explains "why" it's not a good idea;

    It's because array is treated as a constant pointer in the function it is declared. There is a reason for it. Array variable is supposed to point to the first element of the array or first memory instance of the block of the contiguous memory locations in which it is stored. So, if we will have the liberty to to change(increment or decrement ) the array pointer, it won't point to the first memory location of the block. Thus it will loose it's purpose.

提交回复
热议问题