被迫将可变长度数组功能用于打印方阵的辅助函数,我将其定义如下:
void print_matrix(M, dim)
unsigned dim;
int M[dim][dim];
{
/* Print the matrix here. */
...
Run Code Online (Sandbox Code Playgroud)
好消息是,代码可以正常工作,并且其参数按照我希望的顺序排列。
dim坏消息是,我必须使用“旧式”函数声明语法才能引用的声明中尚未声明的参数M,这显然被认为是过时且危险的。
是否有一种直接的方法可以对“新式”函数声明执行相同的操作而不更改参数的顺序?(如果不是,在这种特殊情况下使用旧式语法是否可以接受?)
这个问题建立在这个问题的基础上,它描述了以下内容如何等效:
int f(int a[10]) { ... } // the 10 makes no difference
int f(int a[]) { ... }
int f(int *a) { ... }
Run Code Online (Sandbox Code Playgroud)
在有关函数原型作用域的文档中,提供了以下示例:
int f(int n, int a[n]); // n is in scope, refers to first param
Run Code Online (Sandbox Code Playgroud)
这让我质疑以下内容在多大程度上是等效的:
// 1.
int f(int n, int a[n]) { ... }
int f(int n, int *a) { ... }
// my guess: exactly equivalent
// 2.
int x = 10; int f(int a[x]) { ... } …Run Code Online (Sandbox Code Playgroud) c arrays language-lawyer implicit-conversion function-declaration