【问题标题】:C: finding the number of elements in an array[]C:查找数组中的元素个数[]
【发布时间】:2010-11-02 18:50:31
【问题描述】:

在 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 不同。 我需要知道数组中有多少元素。

【问题讨论】:

  • 因为sizeof() 是编译时运算符,而不是成员函数(如在 C++ 中)。

标签: c arrays pointers


【解决方案1】:

你不能。

你必须将大小传递给函数,例如:

void f(myStruct* array, size_t siz);

还要注意f 中的数组是一个指针,而main 中它是一个数组。数组和指针是不同的东西。

【讨论】:

  • 为什么使用size_t?您不能只使用整数作为数组长度,还是不安全?
  • @Christian Mann:有几个原因,包括:sizeof返回的值的类型是size_t;数组大小永远不能为负数,因此对数组大小使用无符号类型是有意义的; size_t 保证足够宽以容纳可以在任何时候分配的最大字节数,而 int 不是(IOW,如果 T 的数组可以包含 2^64 字节,则 size_t保证为 64 位宽,但 int 仅保证至少为 16 位宽);
  • 传递大小的另一种方法是 NULL 终止数组,这是一个久经考验的解决方案,但这仅适用于您有一个指针数组(这比C)。
【解决方案2】:

在 f array 是一个指针,在 main array 是一个数组。

【讨论】:

  • 而且一旦有了指针就无法确定元素的数量。
【解决方案3】:

您必须将该数据作为单独的参数传递给函数。在 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]);

【讨论】:

    【解决方案4】:

    请注意,在 main() 中,array 指的是实际的数组,因此 sizeof() 给出了所需的答案。

    但是当你将它作为函数参数传递时,你实际上传递的是存储在指针变量'array'中的数组第一个元素的地址。
    所以现在 sizeof() 给出了指针变量的大小,这就是它与实际答案不同的原因。

    可能的解决办法是

    1.全局声明数组

    2.将数组大小作为函数参数传递 希望对您有所帮助!

    【讨论】:

      【解决方案5】:

      在上面的代码中,函数 f() 无法知道原始数组中有多少元素。这是语言的一个特性,没有办法绕过它。你必须通过长度。

      【讨论】:

        【解决方案6】:

        正如C reference 所说,除非最后一个元素是唯一的,或者您将数组元素的计数传递给函数,否则您不能这样做。

        【讨论】:

          【解决方案7】:

          你必须以一个特殊的值结束数组,并且在被调用的函数中你必须计数到那个值,这就是 strlen() 的工作原理,它计数到 NULL '\0' 值。

          【讨论】:

          • 这当然是一种解决方案,但不是唯一的。
          • 您正在向函数发送一个指针(内存中的地址),如果它不知道它在哪里结束,它将如何计数。正如我们在高级语言中所知道的那样,C 中没有数组类型。您可以在 C++ 中创建一个数组类,该类还存储其大小(或使用所有已定义的)
          【解决方案8】:

          您无法始终如一地判断 C 中数组中的元素数量。特别是如果你通过指针传递数组。

          通常,如果您必须在函数中使用数组大小​​,请将其作为参数传递给它。

          【讨论】:

          • 数组中的元素个数始终由sizeof array / sizeof array[0] 给出。只要数组是一个诚实的数组就很容易:)
          【解决方案9】:

          当您在main 中使用sizeof 时,它会计算数组,并给出实际数组的大小。

          当您在f 中使用sizeof 时,您已将数组的名称作为参数传递给函数,因此它已衰减为指针,因此sizeof 会告诉您指针的大小.

          一般来说,如果将数组传递给函数,则需要将函数编写为仅使用特定大小的数组,或者显式传递数组的大小以使其在特定调用中使用。

          【讨论】:

            【解决方案10】:

            您可以为数组使用一种格式。我正在使用字符串元素,它应该适用于结构。

            #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);
            

            【讨论】:

              【解决方案11】:

              作为解决方案的示例:

              给定

              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
                          }
                      }
                  }
              
              };
              

              在main()中

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

              给你正确的答案。

              【讨论】:

                【解决方案12】:
                int array[10]
                
                int Number_of_elements = sizeof(array) / sizeof(array[0]);
                
                printf("%d", Number_of_elements);
                

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 2021-04-19
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2019-12-06
                  • 2020-10-26
                  • 2015-02-14
                  相关资源
                  最近更新 更多