Ger*_*ard 33 c c++ syntax matrix
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;
}
Run Code Online (Sandbox Code Playgroud)
我的测试矩阵的例子
double in[][3] = {
{ 12, -51, 4},
{ 6, 167, -68},
{ -4, 24, -41},
{ -1, 1, 0},
{ 2, 0, 3},
};
Run Code Online (Sandbox Code Playgroud)
我有点迷茫,我无法弄清楚什么是"int n;" 在我的参数声明中,它适用于C,但C++不允许这种实现.我想了解这是如何工作的,因为我要将此代码迁移到c ++.谢谢
Die*_*Epp 41
它是C99 GNU扩展(GCC文档)中很少使用的功能,用于转发声明VLA声明符中使用的参数.
matrix_* matrix_insert_values(int n; double a[][n], int m, int n);
Run Code Online (Sandbox Code Playgroud)
你看到int n两次出现的情况吗?第一个int n;是实际的前向声明int n,最后是.它必须在之前出现double a[][n]因为n在声明中使用a.如果你可以重新排列参数,你可以放在n之前a然后你不需要这个功能
matrix_* matrix_insert_values_rearranged(int m, int n, double a[][n]);
Run Code Online (Sandbox Code Playgroud)
需要明确的是,GNU扩展只是函数参数的前向声明.以下原型是标准C:
// standard C, but invalid C++
matrix_* matrix_insert_values_2(int m, int n, double a[][n]);
Run Code Online (Sandbox Code Playgroud)
您无法从C++调用此函数,因为此代码使用C++不支持的可变长度数组.您必须重写该函数才能从C++中调用它.