所以你可以这么做:
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);对吗?
发布于 2011-08-31 16:11:46
不,你的上一个变体是不对的。您要做的工作是通过以下新语法在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限定符是“无用的”,因为他们限定了实际参数的副本。我一点也不认为它们是无用的,但就我个人而言,我没有遇到太多在现实生活中使用它们的理由。
发布于 2011-08-31 16:12:31
使用cdecl。它在第二个条目上出现了一个错误。第一个只清楚地表明,第二个const指的是*。
发布于 2011-08-31 16:44:47
在C/C++中,不能将整个数组作为参数传递给函数。但是,您可以通过在没有索引的情况下指定数组的名称,将指向数组的指针传递给函数。
(例如)这个程序片段将i的地址传递给func1():
int main(void)
{
int i[10];
func1(i);
.
.
.
}要接收i,可以将名为func1()的函数定义为
void func1(int x[]) /* unsized array */
{
.
.
}或
void func1(int *x) /* pointer */
{
.
.
}或
void func1(int x[10]) /* sized array */
{
.
.
}资料来源:完整参考资料-赫伯特。
https://stackoverflow.com/questions/7259948
复制相似问题