1

メイン関数に 2 つの型 double 2D 配列があります。array1 を array2 にコピーする関数があります。私は両方の配列をその関数に渡しています。最初の配列は変更する必要がないので、最初の配列の仮パラメーターを const 型として宣言していますが、MinGW GCC は ma に警告を出します:

main.c:14:5: warning: passing argument 1 of 'Copy' from incompatible pointer type [enabled by default]
main.c:2:6: note: expected 'const double (*)[3]' but argument is of type 'double (*)[3]'

プログラムはその警告以外にも機能しますが、型 const から読み取るだけの場合、型の仮パラメーターを宣言しても問題ありませんか。改竄されるという実論を守りたい。

#include <stdio.h>
void Copy( const double (*)[3], double (*)[3], int);

int main(void)
{
    double array[4][3] =
    { 
        {79.2, 12.6, 111.9}, 
        {56.4, 139.2, 111.5}, 
        {11.1, 99.7, 21.0}, 
        {91.0, 11.2, 45.5}
    };
    double arrCopy[4][3];
    Copy(array, arrCopy,  4);


    for(int i = 0; i < 4; i++)
    {
        for(int j = 0; j < 3; j++)
        {
            printf("%.2f ", *(*(arrCopy + i) + j));
        }
        putchar('\n');
    }
    return 0;
}

void Copy( const double (*array)[3], double (*arrCopy)[3], int n )
{
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < 3; j++)
        {
            *(*(arrCopy + i) + j) = *(*(array + i) + j);
        }
    }
}
4

1 に答える 1

1

const を削除

void Copy( const double (*array)[3], double (*arrCopy)[3], int n )
于 2013-02-15T14:19:43.367 に答える