首页 > 解决方案 > 为什么 double (*p)[][] 不能是有效参数,而在一维数组中 double (*p)[n] 和 double (*p)[] 可以工作?

问题描述

#include <stdio.h>
void print3(int n, double (*p)[]);
void print2(int n, double (*p)[n]);
void print1(int m, int n, double (*p)[m][n]);
int main(void)
{
    double a[3][2] = {{11.11, 12.12}, {21.21, 22.22}, {31.31, 32.32}};
    print1(3, 2, &a);

    double b[3] = {1234.5, 2234.5, 3234.5};
    print2(3, &b);
    print3(3, &b);

    return 0;
}
void print1(int m, int n, double (*p)[m][n])
{
    for (int i = 0; i < m; i++)
    {
        for (int j = 0; j < n; j++)
            printf("%lf~~~", (*p)[i][j]);
        putchar('\n');
    }
}
void print2(int n, double (*p)[n])
{
    for (int i = 0; i < n; i++)
        printf("%lf~~~", (*p)[i]);
    putchar('\n');
}
void print3(int n, double (*p)[])
{
    for (int i = 0; i < n; i++)
        printf("%lf~~~", (*p)[i]);
    putchar('\n');
}

看上面的代码。

我写了三个函数:print1、print2、print3

这是输出:

11.110000~~~12.120000~~~
21.210000~~~22.220000~~~
31.310000~~~32.320000~~~
1234.500000~~~2234.500000~~~3234.500000~~~
1234.500000~~~2234.500000~~~3234.500000~~~

print2 和 print3 完全相同。

所以我认为double (*p)[n]double (*p)[]都可以用作 C 函数参数。

但是,我在double (*p)[m][n]中删除 m 和 n 后,程序无法编译。

所以让我感到困惑的是:为什么双 (*p)[][]不能是一个有效的参数,而在一维数组中双 (*p)[n]双 (*p)[]可以工作?

标签: carraysfunctionarguments

解决方案


推荐阅读