2011-04-30 9 views
0
 
#define rows 2 
#define cols 2 
#define NUM_CORNERS 4 

int main(void) { 
    int i; 
    int the_corners[NUM_CORNERS]; 
    int array[rows][cols] = {{1, 2}, {3, 4}}; 
    corners(array, the_corners); 
    for (i = 0; i < 4; i++) printf("%d\n", the_corners[i]); 
} 

int corners (int array[rows][cols], int the_corners[]) { 
    the_corners = { 
     array[0][cols-1], 
     array[0][0], 
     array[rows-1][0], 
     array[rows-1][cols-1] 
    }; 
} 

は、私はこれらの奇妙なエラーを取得し、私はその理由は考えていません:メインは、「あなたの機能について知っているdoesnのGCCはなぜ "表現を期待する"のですか?

prog.c: In function ‘main’: 
prog.c:10: warning: implicit declaration of function ‘corners’ 
prog.c: In function ‘corners’: 
prog.c:15: error: expected expression before 
+1

外部リンクを使用するのではなく、ここにコードを貼り付けてください。 – razlebe

+0

次回は大丈夫です。 – tekknolagi

+0

すべての#def定数に大文字を使用することをお勧めします。人々は通常、小文字のもの、特に関数でない場合はプリプロセッサのマクロ/定数であるとは考えていません。 – ThiefMaster

答えて

2

the_corners = { ... }構文は配列の初期化であり、割り当てではありません。私は章や詩を引用することはできませんので、私は、標準的な便利のコピーを持っていないが、あなたはこれを言いたい:あなたが戻っていなかったとして

void corners (int array[rows][cols], int the_corners[]) { 
    the_corners[0] = array[0][cols-1]; 
    the_corners[1] = array[0][0]; 
    the_corners[2] = array[rows-1][0]; 
    the_corners[3] = array[rows-1][cols-1]; 
} 

は私もvoid cornersint cornersを変更する自由を取りました何でもmainにも戻り値が必要で、忘れたのは#include <stdio.h>です。

+0

私はstdio.hを含む私自身のライブラリを含んでいるので、インクルードを外しました – tekknolagi

+0

@tekknolagi:Fair十分な、私はあなたのコンパイラの警告フラグをすべて有効にしていない(または警告を無視していた)と思います。 –

0

を。メインの上に関数の解読を移動するか、プロトタイプをメインの前に移動します。

int corners (int array[rows][cols], int the_corners[NUM_CORNERS]); 
+0

大丈夫です、それはあまり意味がありません....まだ '{'トークンの前に式を期待しています – tekknolagi

2

割り当てとして初期化式を使用しようとしています。 the_cornersのタイプがint*であり、int[4]ではないため、これはC99でも有効ではありません。この場合、各要素を個別に割り当てるのが最良です。

0

は、この方法を試してください。

#include <stdio.h> 
#define NROWS 2 
#define NCOLUMNS 2 
#define NCORNERS 4 

int corners(int (*arr)[NCOLUMNS], int* the_corners); 

int main() { 
    int i; 
    int the_corners[NCORNERS]; 
    int arr[NCOLUMNS][NROWS] = {{1, 2}, {3, 4}}; 

    corners(arr, the_corners); 

    for (i = 0; i < NCORNERS; i++) 
     printf("%d\n", the_corners[i]); 

    return 0; 
} 

int corners(int (*arr)[NCOLUMNS], int* the_corners) { 

     the_corners[0] = arr[0][NCOLUMNS-1]; 
     the_corners[1] = arr[0][0]; 
     the_corners[2] = arr[NROWS-1][0]; 
     the_corners[3] = arr[NROWS-1][NCOLUMNS-1]; 

     return 0; 
} 

あなたが関数に2次元配列を渡すについてhereを読むことができます。

関連する問題