Why can't I increment an array?

前端 未结 5 1183
野的像风
野的像风 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:26

    No, it's not OK to increment an array. Although arrays are freely convertible to pointers, they are not pointers. Therefore, writing a++ will trigger an error.

    However, writing

    char *p = a;
    p++;
    

    is fine, becuase p is a pointer, with value equal to the location of a's initial element.

提交回复
热议问题