2010-11-02 69 views
13

In C: 在將數組發送到函數後,如何查找結構數組中的元素數?C:查找數組中元素的數量[]

int main(void) { 
    myStruct array[] = { struct1, struct2, struct3, struct4, struct5, struct6 }; 
    printf("%d\n", sizeof(array)); 
    printf("%d\n", sizeof(array[0])); 
    f(array); 
} 
void f(myStruct* array) { 
    printf("%d\n", sizeof(array)); 
    printf("%d\n", sizeof(array[0])); 
} 

出於某種原因,printf在main中顯示的結果與f中的printf不同。 我的需求是知道數組中有多少個元素。

+0

因爲'sizeof()'是一個編譯時操作符,而不是成員函數(如在C++中)。 – ruslik 2010-11-02 22:20:37

回答

18

你不能。

你必須大小傳遞給該函數,例如:

void f(myStruct* array, size_t siz); 

還要注意,在f陣列是指針,而在main它是一個數組。數組和指針是不同的東西。

+1

爲什麼使用'size_t'?難道你不能僅僅使用一個整數來表示數組的長度,還是那個不安全? – 2010-11-02 19:09:17

+2

@Christian Mann:幾個原因,包括:'sizeof'返回的值的類型是'size_t';數組的大小決不能是負數,所以使用數組大小​​的無符號類型是有意義的; 'size_t'保證足夠寬以容納任何時候可以分配的最大字節數,而'int'不是(IOW,如果一個T數組可以包含2^64個字節,那麼'size_t'保證是64位寬,但'int'只能保證至少16位寬); – 2010-11-02 19:16:11

+0

傳遞大小的替代方法是NULL終止數組,這是一個經過時間考驗的解決方案,但只有當您有指針數組(這比C中的數組數組更常見)時纔有效。 – 2010-11-02 20:14:11

10

駐f array是指針,在主array是一個數組。

+5

一旦你有一個指針,你不能確定元素的數量。 – 2010-11-02 18:53:23

0

不能在Ç一直告訴數組中元素的個數。特別是如果你通過指針傳遞數組。

通常,如果你必須在函數中使用數組大小​​,請將它作爲參數傳遞給它。

+1

數組中元素的數量總是由sizeof array/sizeof array [0]'給出的。只要數組是一個誠實的數組,它就很容易:) – pmg 2010-11-02 18:57:46

1

您必須將該數據作爲單獨的參數傳遞給函數。在C和C++中,只要數組傳遞給函數,數組就會退化爲一個指針。指針沒有指出它們指向的數組中有多少個元素。

一種常見的方式得到大小爲聲明數組,然後立即通過由一個元件的尺寸將總大小獲取數組元素計數。像這樣:

struct my_struct my_struct_array[] = { 
{"data", 1, "this is data"}, 
{"more data", 2, "this is more data"}, 
{"yet more", 0, "and again more data"} 
}; 
const size_t my_struct_array_count = sizeof(my_struct_array)/sizeof(my_struct_array[0]); 
0

當您使用mainsizeof,它的評估陣列,並給出了實際的數組的大小。

當您使用fsizeof,你已經通過了數組作爲參數傳遞給函數的名稱,因此它已經衰減到一個指針,所以sizeof告訴你一個指針的大小。

一般而言,如果您將數組傳遞給函數,則需要編寫該函數以僅使用一個特定大小的數組,或顯式傳遞數組的大小以使其與特定調用一起使用。

1

在上面的代碼中,函數f()無法知道原始數組中有多少個元素。這是該語言的一個特點,無法繞過它。你必須通過這個長度。

1

隨着C reference說,你不能這樣做除非是最後一個元素是唯一的,或者你通過數組元素的函數的計數。

1

你必須結束與一個特殊值的數組和調用的函數,你要算到那個值,它是如何的strlen()的作品計數到NULL「\ 0」值。

+0

這當然是一種解決方案,但它不是唯一的解決方案。 – 2010-11-02 20:33:59

+0

您正在發送函數指針(內存中的地址),它將如何計數,如果它不知道它在哪裏結束。正如我們在高級語言中所知,C中沒有數組類型。您可以使用C++創建一個數組類,該類還可以存儲其大小(或使用所有已定義的大小) – bkilinc 2010-11-02 21:33:52

0

您可以使用陣列的格式。我使用字符串元素,它應該爲結構工作。

#define NULL "" 
#define SAME 0 

static char *check[] = { 
     "des", "md5", "des3_ede", "rot13", "sha1", "sha224", "sha256", 
     "blowfish", "twofish", "serpent", "sha384", "sha512", "md4", "aes", 
     "cast6", "arc4", "michael_mic", "deflate", "crc32c", "tea", "xtea", 
     "khazad", "wp512", "wp384", "wp256", "tnepres", "xeta", "fcrypt", 
     "camellia", "seed", "salsa20", "rmd128", "rmd160", "rmd256", "rmd320", 
     "lzo", "cts", "zlib", NULL 
}; // 38 items, excluding NULL 
在main()

char **algo = check; 
int numberOfAlgo = 0; 


while (SAME != strcmp(algo[numberOfAlgo], NULL)) { 
    printf("Algo: %s \n", algo[numberOfAlgo++]); 
} 

printf("There are %d algos in the check list. \n", numberOfAlgo); 

你應該得到的輸出

Algo: des 
    : 
    : 
Algo: zlib 

There are 38 algos in the check list. 

另外,如果你不想使用NULL,而是執行此操作:

numberOfAlgo = 0; 

while (*algo) { 
    printf("Algo: %s \n", *algo); 
    algo++;   // go to the next item 
    numberOfAlgo++; // count the item 
} 

printf("There are %d algos in the check list. \n", numberOfAlgo); 
0

舉個例子,以您的解決方案:

鑑於

struct contain { 
char* a;  // 
int allowed; // 

struct suit { 
    struct t { 
      char* option; 
      int count; 
    } t; 

    struct inner { 
      char* option; 
      int count; 
    } inner; 
} suit; 
}; 

//如。在主初始化

 struct contain structArrayToBeCheck[] = { 
    { 
     .a = "John", 
     .allowed = 1, 

     .suit = { 
      .t = { 
       .option = "ON", 
       .count = 7 
      }, 

      .inner = { 
       .option = "OFF", 
       .count = 7 
      } 
     } 
    }, 
    { 
     .a = "John", 
     .allowed = 1, 

     .suit = { 
      .t = { 
       .option = "ON", 
       .count = 7 
      }, 

      .inner = { 
       .option = "OFF", 
       .count = 7 
      } 
     } 
    }, 
    { 
     .a = "John", 
     .allowed = 1, 

     .suit = { 
      .t = { 
       .option = "ON", 
       .count = 7 
      }, 

      .inner = { 
       .option = "OFF", 
       .count = 7 
      } 
     } 
    }, 
    { 
     .a = "John", 
     .allowed = 1, 

     .suit = { 
      .t = { 
       .option = "ON", 
       .count = 7 
      }, 

      .inner = { 
       .option = "OFF", 
       .count = 7 
      } 
     } 
    } 

}; 

()

printf("Number of Struct within struct array: %d \n", sizeof(structArrayToBeCheck)/sizeof(struct contain)); 

給你正確的答案。

1

請注意,在main()中,數組引用實際數組,因此sizeof()會提供所需的答案。

但是,當您將它作爲函數參數傳遞時,您實際上正在傳遞存儲在指針變量「數組」中的數組的第一個元素的地址。
所以現在sizeof()給出了指針變量的大小,這就是它與實際答案不同的原因。

可能的解決方案可以是

1.Declare陣列全球

2.Pass數組的大小作爲函數參數 希望它能幫助!