const数组const {} [英] const array const {}

查看:50
本文介绍了const数组const {}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,您可以执行以下操作:

So you can do this:

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

其中说来的指针是只读的,指向的整数是只读的

Which says that the pointer coming is read-only and the integer's it is pointing to are read-only.

您可以在函数内部访问此代码,如下所示:

You can access this inside the function like so:

blah = pIntArray[0]

您还可以执行以下声明:

You can also do the following declaration:

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

这几乎是相同的,但是您可以这样做:

It is pretty much the same but you could do this:

intArray = &intArray[1];

我可以这样写吗:

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

对吗?

推荐答案

否,您的最后一个变体不正确。您正在尝试通过以下新语法在C99中实现

No, your last variant is not correct. What you are trying to do is achieved in C99 by the following new syntax

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

等效于

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

[const] 语法特定于C99。在C89 / 90中无效。

That [const] syntax is specific to C99. It is not valid in C89/90.

请记住,有些人认为函数参数的顶级cv限定词无用,因为它们限定了复制实际参数。我认为它们根本没有用,但就我个人而言,在现实生活中我不会遇到太多使用它们的原因。

Keep in mind that some people consider top-level cv-qualifiers on function parameters "useless", since they qualify a copy of the actual argument. I don't consider them useless at all, but personally I don't encounter too many reasons to use them in real life.

这篇关于const数组const {}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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