次の問題を引き起こしているのはアレイです。
void print_graph(g_node graph_node[], double weight[][], int nodes);
2番目以降の次元を指定する必要があります。
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()で、関数に渡そうとしている変数は、ですがdouble array[][]
、それを関数に渡すにはどうすればよいですか?それに渡すと、andと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(Apple Inc.ビルド5658に基づく)(LLVMビルド2336.9.00))およびGCCで(オブジェクトコードに)クリーンにコンパイルされます。コマンドラインを使用したMacOSX10.7.3の4.7.0:
/usr/bin/gcc -O3 -g -std=c99 -Wall -Wextra -c zzz.c