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

时间:2010-11-02 18:50:31

标签: 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", sizeof(array));
  printf("%d\n", sizeof(array[0]));
}

由于某种原因,main中的printf显示的结果与f中的printf不同。 我需要知道数组中有多少元素。

12 个答案:

答案 0 :(得分:19)

你不能。

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

void f(myStruct* array, size_t siz);

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

答案 1 :(得分:10)

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

答案 2 :(得分:1)

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

答案 3 :(得分:1)

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

答案 4 :(得分:1)

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

答案 5 :(得分:1)

你必须使用特殊值结束数组,并且在被调用函数中,你必须计算到strlen()工作的值,它会计算为NULL'\ 0'值。

答案 6 :(得分:1)

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

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

可能的解决办法是

1.全局声明数组

2.将数组大小作为函数参数 希望它有所帮助!

答案 7 :(得分:0)

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

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

答案 8 :(得分:0)

sizeof中使用main时,它正在评估数组,并给出实际数组的大小。

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

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

答案 9 :(得分: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

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

答案 10 :(得分:0)

作为解决方案的示例:

鉴于

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

给出正确答案。

答案 11 :(得分:0)

int array[10]

int Number_of_elements = sizeof(array) / sizeof(array[0]);

printf("%d", Number_of_elements);
相关问题