代码之家  ›  专栏  ›  技术社区  ›  Lavi Avigdor

C: 查找数组中的元素数[]

  •  13
  • Lavi Avigdor  · 技术社区  · 14 年前

    在将一个结构数组发送到一个函数之后,如何在它的数组中找到元素的数目?

    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-in-main显示的结果与printf-in-f不同。 我需要知道数组中有多少元素。

    11 回复  |  直到 14 年前
        1
  •  19
  •   pmg    14 年前

    你不能。

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

    void f(myStruct* array, size_t siz);
    

    还要注意 f 数组是指针,而在 main

        2
  •  10
  •   lavinio    14 年前

    在f中 array 是一个指针 阵列

        3
  •  2
  •   Zan Lynx    14 年前

    必须将该数据作为单独的参数传递给函数。在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
  •  2
  •   Apaar    9 年前

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

    1.全局声明数组

    希望有帮助!

        5
  •  1
  •   Jean-Marc Valin    14 年前

        6
  •  1
  •   David Harris    14 年前

    作为 C reference 也就是说,除非最后一个元素是唯一的,或者向函数传递数组元素的计数,否则无法执行此操作。

        7
  •  1
  •   bkilinc    14 年前

    你必须用一个特殊的值结束数组,在被调用的函数中,你必须数到这个值,这就是strlen()的工作原理,它最多可以数到空的“\0”值。

        8
  •  0
  •   Pablo Santa Cruz    14 年前

    不能在 C类

    通常,如果你 必须

        9
  •  0
  •   Jerry Coffin    14 年前

    当你使用 sizeof main ,它正在计算数组,并给出实际数组的大小。

    尺寸 f ,您已将数组的名称作为参数传递给函数,因此它已衰减为指针,因此 尺寸

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

        10
  •  0
  •   Ursa Major    11 年前

    可以为数组使用格式。我使用的是字符串元素,它应该适用于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.
    

    或者,如果您不想使用 无效的 ,请改为:

    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
  •  0
  •   Ursa Major    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
                }
            }
        }
    
    };
    

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

    给你正确的答案。