加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

const数组const {}

发布时间:2020-12-16 04:57:10 所属栏目:百科 来源:网络整理
导读:所以你可以这样做: void foo(const int * const pIntArray,const unsigned int size); 这表示指针是只读的,它指向的整数是只读的. 你可以像这样访问函数内部: blah = pIntArray[0] 您还可以执行以下声明: void foo(const int intArray[],const unsigned i
所以你可以这样做:
void foo(const int * const pIntArray,const unsigned int size);

这表示指针是只读的,它指向的整数是只读的.

你可以像这样访问函数内部:

blah = pIntArray[0]

您还可以执行以下声明:

void foo(const int intArray[],const unsigned int size);

它几乎是一样的,但你可以这样做:

intArray = &intArray[1];

我可以写:

void foo(const int const intArray[],const unsigned int size);

那是对的吗?

解决方法

不,你的最后一个变种是不正确的.您尝试做的是通过以下新语法在C99中实现的
void foo(const int intArray[const],const unsigned int size);

这相当于

void foo(const int *const intArray,const unsigned int size);

那个[const]语法特定于C99.它在C89 / 90中无效.

请记住,有些人认为函数参数上的顶级cv限定符“无用”,因为它们限定了实际参数的副本.我认为它们毫无用处,但我个人认为在现实生活中使用它们并没有太多理由.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读