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])); }
由于某种原因,main中的printf显示的结果与f中的printf不同。 我需要知道数组中有多少个元素。
你不能。
你必须将大小传递给函数,例如:
void f(myStruct* array, size_t siz);
另请注意,在f
数组中是一个指针,而在main
它是一个数组。 数组和指针是不同的东西。
在f array
是一个指针,在主array
是一个数组。
您必须将该数据作为单独的参数传递给该函数。 在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]);
在上面的代码中,函数f()无法知道原始数组中有多少元素。 这是该语言的一个特点,没有办法绕过它。 你必须通过这个长度。
正如C引用所说,除非最后一个元素是唯一的,否则你不能这样做,或者将数组元素的数量传递给函数。
你必须用一个特殊的值来结束数组,并且在被调用的函数中,你必须计算到strlen()工作的那个值,它计算为NULL’\ 0’值。
请注意,在main()中,数组引用实际数组,因此sizeof()给出了所需的答案。
但是当你将它作为函数参数传递时,实际上是传递了存储在指针变量“array”中的数组的第一个元素的地址。
所以现在sizeof()给出了指针变量的大小,这就是它与实际答案不同的原因。
可能的解决办法是
1.全局声明数组
2.通过数组大小作为function参数希望它有所帮助!
您无法一致地判断C中数组中的元素数量。 特别是如果你通过指针传递数组。
通常,如果必须在函数中使用数组大小,请将其作为参数传递给它。
在main
使用sizeof
时,它会评估数组,并给出实际数组的大小。
当您在f
使用sizeof
时,您已将数组的名称作为参数传递给函数,因此它已衰减为指针,因此sizeof
告诉您指针的大小。
一般来说,如果将数组传递给函数,则需要将函数编写为仅使用一个特定大小的数组,或者显式传递数组的大小以使其适用于特定的调用。
您可以使用arrays的格式。 我正在使用字符串元素,它应该适用于struct。
#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
在主()
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);
作为您的解决方案的示例:
特定
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));
给你正确的答案。