您可以使用陣列的格式。我使用字符串元素,它應該爲結構工作。
#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);
因爲'sizeof()'是一個編譯時操作符,而不是成員函數(如在C++中)。 – ruslik 2010-11-02 22:20:37