为什么不能递增数组? [英] Why can't I increment an array?

查看:99
本文介绍了为什么不能递增数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆