matrix_* matrix_insert_values(int n; double a[][n], int m, int n)
{
matrix_* x = matrix_new(m, n);
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
x->v[i][j] = a[i][j];
return x;
}
我的测试矩阵示例
double in[][3] = {
{ 12, -51, 4},
{ 6, 167, -68},
{ -4, 24, -41},
{ -1, 1, 0},
{ 2, 0, 3},
};
我有点迷茫,我无法弄清楚我的参数声明中的 int n;
是什么,它适用于 C,但 C++ 不允许这种实现。我想了解这是如何工作的,因为我要将此代码迁移到 C++。
最佳答案
这是 C99 中很少使用的 功能 GNU 扩展 (GCC documentation),用于前向声明 VLA 声明器中使用的参数。
matrix_* matrix_insert_values(int n; double a[][n], int m, int n);
你看到 int n
出现两次了吗?第一个 int n;
只是实际 int n
的前向声明,它位于末尾。它必须出现在 double a[][n]
之前,因为在 a
的声明中使用了 n
。如果你对重新排列参数没问题,你可以把 n
放在 a
之前,然后你就不需要这个功能了
matrix_* matrix_insert_values_rearranged(int m, int n, double a[][n]);
关于 C++ 兼容性的注意事项
需要明确的是,GNU 扩展只是函数参数的前向声明。以下原型(prototype)是标准C:
// standard C, but invalid C++
matrix_* matrix_insert_values_2(int m, int n, double a[][n]);
您不能从 C++ 调用此函数,因为此代码使用了 C++ 不支持的可变长度数组。您必须重写该函数才能从 C++ 调用它。
关于c++ - 函数参数上的分号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15145524/