2015-12-07 60 views
6

我需要一些幫助來計算二維數組的行和列。看來我不能列數列?C:二維數組的大小

#include <stdio.h> 

int main() { 

char result[10][7] = { 

    {'1','X','2','X','2','1','1'}, 
    {'X','1','1','2','2','1','1'}, 
    {'X','1','1','2','2','1','1'}, 
    {'1','X','2','X','2','2','2'}, 
    {'1','X','1','X','1','X','2'}, 
    {'1','X','2','X','2','1','1'}, 
    {'1','X','2','2','1','X','1'}, 
    {'1','X','2','X','2','1','X'}, 
    {'1','1','1','X','2','2','1'}, 
    {'1','X','2','X','2','1','1'} 

}; 

int row = sizeof(result)/sizeof(result[0]); 
int column = sizeof(result[0])/row; 

printf("Number of rows: %d\n", row); 
printf("Number of columns: %d\n", column); 

} 

輸出:
行數:10
列數:0

+3

'INT列=的sizeof(結果[0])/ sizeof(result [0] [0]);' – BLUEPIXY

+1

既然它們是靜態的,那你爲什麼要數它們呢?只需定義行和列大小的常量,而不是使用「幻數」。 – Lundin

回答

4

這是整數除法的問題!

int column = sizeof(result[0])/row; 

應該是

int column = 7/10; 

和整數除法,7/10==0

你想要做的是劃分一行的長度,例如。例如,該行的一個元素的大小。 sizeof(result[0][0])

int column = sizeof(result[0])/sizeof(result[0][0]); 
+1

這是錯誤的。在這裏,您將7(第一行元素的數量)除以1(result [0] [0]'上的字符數)。 – emi

5

這對我的作品(評論解釋爲什麼):

#include <stdio.h> 

int main() { 

    char result[10][7] = { 

     {'1','X','2','X','2','1','1'}, 
     {'X','1','1','2','2','1','1'}, 
     {'X','1','1','2','2','1','1'}, 
     {'1','X','2','X','2','2','2'}, 
     {'1','X','1','X','1','X','2'}, 
     {'1','X','2','X','2','1','1'}, 
     {'1','X','2','2','1','X','1'}, 
     {'1','X','2','X','2','1','X'}, 
     {'1','1','1','X','2','2','1'}, 
     {'1','X','2','X','2','1','1'} 

    }; 

    // 'total' will be 70 = 10 * 7 
    int total = sizeof(result); 

    // 'column' will be 7 = size of first row 
    int column = sizeof(result[0]); 

    // 'row' will be 10 = 70/7 
    int row = total/column; 

    printf("Total fields: %d\n", total); 
    printf("Number of rows: %d\n", row); 
    printf("Number of columns: %d\n", column); 

} 

和這個輸出是:

Total of fields: 70 
Number of rows: 10 
Number of columns: 7 

編輯:

如指出的通過@AnorZaken,將數組作爲參數傳遞給一個函數並在其上打印sizeof的結果,將會輸出另一個total。這是因爲當你傳遞一個數組作爲參數時(不是指向它的指針),C會將它作爲副本傳遞,並且會在它們之間應用一些C魔法,所以你沒有像你認爲的那樣傳遞完全一樣的東西。爲了確定你在做什麼以及爲了避免一些額外的CPU工作和內存消耗,最好通過引用傳遞數組和對象(使用指針)。所以,你可以使用這樣的事情,有相同的結果。原:

#include <stdio.h> 

void foo(char (*result)[10][7]) 
{ 
    // 'total' will be 70 = 10 * 7 
    int total = sizeof(*result); 

    // 'column' will be 7 = size of first row 
    int column = sizeof((*result)[0]); 

    // 'row' will be 10 = 70/7 
    int row = total/column; 

    printf("Total fields: %d\n", total); 
    printf("Number of rows: %d\n", row); 
    printf("Number of columns: %d\n", column); 

} 

int main(void) { 

    char result[10][7] = { 

     {'1','X','2','X','2','1','1'}, 
     {'X','1','1','2','2','1','1'}, 
     {'X','1','1','2','2','1','1'}, 
     {'1','X','2','X','2','2','2'}, 
     {'1','X','1','X','1','X','2'}, 
     {'1','X','2','X','2','1','1'}, 
     {'1','X','2','2','1','X','1'}, 
     {'1','X','2','X','2','1','X'}, 
     {'1','1','1','X','2','2','1'}, 
     {'1','X','2','X','2','1','1'} 

    }; 

    foo(&result); 

    return 0; 
} 
+2

代碼只是沒有解釋的答案通常對未來的訪問者非常無益。考慮編輯您的答案,以提供有關您的解決過程的進一步信息/見解。 – Magisch

+4

代碼對我來說是自動解釋,但理解你。現在編輯答案。 – emi

+0

只是指出這不適用於作爲函數參數接收的數組,特別是'total = sizeof result;'將不起作用:它將生成警告並評估爲單個元素的大小而不是整個陣列。因此,要麼VLA是必需的(並且您傳遞行和列參數),或者您必須將行或總計數作爲參數傳遞(列仍可以用sizeof確定)。 – AnorZaken

6

它更方便(且不易出錯)使用數組長度的宏:

#include <stdio.h> 

#define LEN(arr) ((int) (sizeof (arr)/sizeof (arr)[0])) 

int main(void) 
{ 
    char result[10][7]; 

    printf("Number of rows: %d\n", LEN(result)); 
    printf("Number of columns: %d\n", LEN(result[0])); 
    return 0; 
} 
+0

爲什麼在'LEN'宏上投射整數?不是數組的同構數據結構,即分子將始終是分母的倍數?另外,不應該返回一個'unsigned integer'或size_t',它已經是一些整型? – Rafa

+1

@Rafa如果我們將格式說明符'%d'更改爲'%lu',我們可以不進行強制轉換。當在'for'循環守衛中使用* LEN *時,我們需要使用一個強制類型或者用* size_t *類型聲明索引變量。 –