c - 为什么 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)[]可以工作?
解决方案
推荐阅读
- css - 使用 IOS 或其他移动设备单击元素时,SVG 图像未更改。在 PC / Mac 上运行良好
- typescript - vuex 懒加载数据
- c++ - 在 OpenGL 中以阻塞方式渲染纹理(视频),因此我收到的视频帧在渲染时不会被新帧替换
- laravel - 在 Laravel 中为图像添加水印
- c# - 如何从下到上对列数据进行排序
- javascript - 按下退格键或删除按钮时如何删除整个超链接?
- javascript - 使用 fs.readFileSync 读取文件并将其与包含多行的字符串模板进行比较时出错
- python - 无法解决在 python 中的 selenium webdriver 中导入密钥的错误
- mysql - 创建 Kafka Connect 连接器以监控 AWS MSK(Apache Kafka 托管流)集群中的 AWS RDS MySQL 数据库
- ios - 在 Swift 中更改控制台打印的颜色输出