为什么不能递增数组? [英] Why can't I increment an array?
本文介绍了为什么不能递增数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
char a[] = "hello";
我的理解是,a
的行为就像一个指向字符串的常量指针.我知道写a++
是行不通的,但是为什么呢?
My understanding is that a
acts like a constant pointer to a string. I know writing a++
won't work, but why?
推荐答案
否,增加数组是不正确的.尽管数组可以自由转换为指针,但它们不是指针.因此,写入a++
会触发错误.
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.
但是,写作
char *p = a;
p++;
很好,因为p
是一个指针,其值等于a
初始元素的位置.
is fine, becuase p
is a pointer, with value equal to the location of a
's initial element.
这篇关于为什么不能递增数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文