C:查找数组中的元素数[]
在C中: 在将结构数组发送到函数后,如何查找其中的元素数C:查找数组中的元素数[],c,arrays,pointers,C,Arrays,Pointers,在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",
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不同。
我需要知道数组中有多少个元素。在f
数组中
是一个指针,在main数组中
是一个数组。你不能在C中一致地说出数组中元素的数量。特别是通过指针传递数组时
通常,如果必须在函数中使用数组大小,请将其作为参数传递给函数。必须将该数据作为单独的参数传递给函数。在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]);
你不能
您必须将大小传递给函数,例如:
void f(myStruct* array, size_t siz);
还要注意,在
f
数组中是指针,而在main
中是数组。数组和指针是不同的。在main
中使用sizeof
时,它计算数组,并给出实际数组的大小
在
f
中使用sizeof
时,您已将数组的名称作为参数传递给函数,因此它已衰减为指针,因此sizeof
会告诉您指针的大小
一般来说,如果将数组传递给函数,则需要编写函数以仅使用一个特定大小的数组,或者显式传递数组大小以使其在特定调用中使用。在上面的代码中,函数f()无法知道原始数组中有多少个元素。这是语言的一个特点,没有办法绕过它。您必须传递长度。如所述,除非最后一个元素是唯一的,或者您将数组元素的计数传递给函数,否则您无法执行此操作。您必须以特殊值结束数组,并且在调用的函数中,您必须将该值计算在内,这就是strlen()的方式它最多可计算空'\0'值。您可以为数组使用一种格式。我使用的是字符串元素,它应该适用于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
主要()
您应该获得以下输出:
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 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
}
}
}
};
大体上
给出正确答案。注意,在main()中,数组指的是实际数组,因此sizeof()给出了所需答案
但当您将其作为函数参数传递时,实际上是在传递存储在指针变量“array”中的数组第一个元素的地址。现在sizeof()给出了指针变量的大小,这就是它与实际答案不同的原因 可能的解决办法是 1.全局声明数组 2.将数组大小作为函数参数传递
希望有帮助 一旦有了指针,就无法确定元素的数量。数组中的元素数量始终由
sizeof array/sizeof array[0]
给出。只要数组是一个诚实的数组,就很容易:)为什么要使用size\u t
?你不能用一个整数作为数组长度吗?或者这不安全吗?@Christian Mann:有几个原因,包括:sizeof
返回的值类型是size\t
;数组大小永远不能为负,因此使用无符号类型作为数组大小是有意义的size\u t
保证足够宽,以容纳在任何时间可以分配的最大字节数,而int
则不是(当然,如果一个t数组可以包含2^64个字节,那么size\u t
保证64位宽,但int
仅保证至少16位宽);传递大小的另一种方法是空终止数组,这是一种经过时间测试的解决方案,但这仅在有指针数组(比C中的数组更常见)的情况下才有效。这当然是一种解决方案,但不是唯一的一种。您向函数发送的是指针(内存中的地址),它将如何计数,如果它不知道它在哪里结束。正如我们在高级语言中所知道的那样,C语言中没有数组类型。您可以在C++中创建一个数组类,它也存储其大小(或者使用所有已准备好的),因为<代码> siZeOf()/Cyto>是编译时运算符,而不是成员函数(如C++)。
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));