C——将函数中的多维数组更改为常量?

C——将函数中的多维数组更改为常量?,c,C,我有以下c功能声明: float Sum2d( const unsigned int nRows, const unsigned int mCols, float arr[nRows][mCols] ) { float sumAll = 0; // I would like to make this change illegal! arr[0][0] = 15; for (int i = 0; i < nRows; i++) for (

我有以下
c
功能声明:

float Sum2d( const unsigned int nRows, const unsigned int mCols, float arr[nRows][mCols] )
{
    float sumAll = 0;

    // I would like to make this change illegal! 
    arr[0][0] = 15;

    for (int i = 0; i < nRows; i++)
        for (int j = 0; j < mCols; j++)
            sumAll += arr[i][j];

    return sumAll;
}
此函数运行良好,但有一个问题:可以在
Sum2d()
函数中更改
arr
的元素


我如何更改<代码> SU2D-()/<代码>原型,以防止对<代码> ARR的元素有任何更改?

< P>我不知道你在用什么编译器,但这对C或C++没有编译。


但不管怎样,只需设置arr const就足够了。

将函数的原型更改为使用const和float

另外,您在数组参数中指定了nRows/nCols,这在C中是不允许的。如果您不知道数组的边界,请使用双指针

这种方法不会阻止函数中的类型转换

#include <stdio.h>

float Sum2d( const unsigned int nRows, const unsigned int mCols, const float arr[][2] )
{
    float sumAll = 0;

    // I would like to make this change illegal! 
    //arr[0][0] = 15;

    for (int i = 0; i < nRows; i++)
        for (int j = 0; j < mCols; j++)
            sumAll += arr[i][j];

    return sumAll;
}

int main()
{
    // define a 2d float array
    float myArr2d[3][2] = {{1,2}, {3,4}, {5,6}};

    // calculate the sum
    float sum = Sum2d(3, 2, (const float (*)[2])myArr2d);

    // print the sum
    printf("%f\n", sum);

    // return 1
    return 1;
}
#包括
浮点Sum2d(常量无符号整数nRows、常量无符号整数mCols、常量浮点arr[][2])
{
float-sumAll=0;
//我想把这个改变定为非法!
//arr[0][0]=15;
对于(int i=0;i
由于您使用的是以下命令行,我想:

gcc <file.c> -o out -std=c99

Running on Debian Squeeze
$ gcc array.c -o array -std=c99

$ gcc --version       
gcc (Debian 4.4.5-8) 4.4.5
Copyright (C) 2010 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO   
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.             
gcc-o输出-std=c99
在Debian挤压机上运行
$gcc array.c-o array-std=c99
$gcc—版本
通用条款(Debian 4.4.5-8)4.4.5
版权所有(C)2010免费软件基金会。
这是自由软件;有关复制条件,请参见源。没有
担保甚至不是为了适销性或适合某一特定目的。

具有
const
限定的多维数组很难处理。基本上,您可以选择在每个调用端强制转换非常量数组,以完全避免此类
const
数组作为参数,或者通过使用一些复杂的宏来进行偏离。这是一个较长的故事,你可以读一读。

声明参数const(这并不是有限地阻止你修改它,而是“使它变得更难”)
float Sum2d(const unsigned int nRows,const unsigned int mCols,const float arr[nRows][mCols])
没有人:当我使用你建议的原型时,我得到以下错误:
file.c:71:警告:从不兼容的指针类型文件传递'Sum2d'的参数3.c:38:注意:应为'const float(*)[(unsigned int)mCols]',但参数类型为'float(*)[2]'
我正在使用
c99
中的
gnu
编译器(
Ubuntu Linux
)mode@Nobody:对不起,你错了。@hexa:VLA作为一个自动变量类型是非常无用的,但是它以指向VLA类型的指针的形式非常有用。它允许编译器在传递指针时为您执行所有难看的多维数组索引算法。当然,如果函数可以内联并且数组大小不变,mults将得到优化。ASD:谢谢。当我使用您建议的原型时,我得到以下错误:file.c:71:警告:从不兼容的指针类型文件传递'Sum2d'的参数3.c:38:注意:应为'const float()[(unsigned int)mCols]”,但参数的类型为'float()[2]@user3262424-您确定吗?我刚试过代码,它的行为就像ASD建议的那样。这也是错误的。“另外,您在数组参数中指定了nRows/nCols,这在C中是不允许的。”是错误的(它是允许的)。另外,
const float(*)[2]
不是指向const限定类型的指针。您不能用
float(*)[2]
初始化它,因为您将尝试用
Y*
初始化
X*
,并且
X
Y
在您的情况下是不相关的(
const float[2]
float[2]
)。它不像C++那样工作。我认为你需要修改如何把数组作为参数传递给函数。“使用双指针”。这是一个城市神话,它导致了许多复杂和容易出错的代码。C非常了解如何处理多维数组,无论何时,都应该使用它们。
gcc <file.c> -o out -std=c99

Running on Debian Squeeze
$ gcc array.c -o array -std=c99

$ gcc --version       
gcc (Debian 4.4.5-8) 4.4.5
Copyright (C) 2010 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO   
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.