这是在以下方面引起麻烦的数组:
void print_graph(g_node graph_node[], double weight[][], int nodes);
必须给出第二个和后续维度:
void print_graph(g_node graph_node[], double weight[][32], int nodes);
或者你可以只给一个指针指针:
void print_graph(g_node graph_node[], double **weight, int nodes);
然而,虽然它们看起来很相似,但它们在内部却大不相同。
如果您使用的是 C99,则可以使用可变限定数组。引用 C99 标准中的一个示例(第 §6.7.5.2 节数组声明器):
void fvla(int m, int C[m][m]); // valid: VLA with prototype scope
void fvla(int m, int C[m][m]) // valid: adjusted to auto pointer to VLA
{
typedef int VLA[m][m]; // valid: block scope typedef VLA
struct tag {
int (*y)[n]; // invalid: y not ordinary identifier
int z[n]; // invalid: z not ordinary identifier
};
int D[m]; // valid: auto VLA
static int E[m]; // invalid: static block scope VLA
extern int F[m]; // invalid: F has linkage and is VLA
int (*s)[m]; // valid: auto pointer to VLA
extern int (*r)[m]; // invalid: r has linkage and points to VLA
static int (*q)[m] = &B; // valid: q is a static block pointer to VLA
}
评论中的问题
[...] 在我的 main() 中,我试图传递给函数的变量是 a double array[][]
,那么我该如何将它传递给函数呢?传入array[0][0]
它会给我不兼容的参数类型,&array
和&array[0][0]
.
在您的main()
中,变量应该是:
double array[10][20];
或类似的东西;也许
double array[][20] = { { 1.0, 0.0, ... }, ... };
您应该能够使用如下代码传递它:
typedef struct graph_node
{
int X;
int Y;
int active;
} g_node;
void print_graph(g_node graph_node[], double weight[][20], int nodes);
int main(void)
{
g_node g[10];
double array[10][20];
int n = 10;
print_graph(g, array, n);
return 0;
}
使用 GCC 4.2 (i686-apple-darwin11-llvm-gcc-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.9.00)) 和 GCC Mac OS X 10.7.3 上的 4.7.0 使用命令行:
/usr/bin/gcc -O3 -g -std=c99 -Wall -Wextra -c zzz.c