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

时间:2022-08-22 14:11:59

In C: How do you find the number of elements in an array of structs, after sending it to a function?

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

For some reason the printf in main shows different results than the printf in f. My need is to know how many elements are in the array.

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

11 个解决方案

#1


19  

You can't.

你不能。

You have to pass the size to the function, eg:

你必须把尺寸传递给这个函数。

void f(myStruct* array, size_t siz);

Also notice that in f array is a pointer, while in main it is an array. Arrays and pointers are different things.

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

#2


10  

In f array is a pointer, in main array is an array.

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

#3


1  

You must pass that data as a separate parameter to the function. In C and C++ as soon as an array is passed to a function the array degenerates into a pointer. Pointers have no notion of how many elements are in the array they point to.

必须将数据作为单独的参数传递给函数。在C和c++中,数组一旦传递给函数,数组就会退化为指针。指针不知道它们指向的数组中有多少个元素。

A common way to get the size is to declare the array and then immediately get the array element count by dividing the total size by the size of one element. Like this:

获取大小的一种常见方法是声明数组,然后通过将总大小除以一个元素的大小立即获得数组元素计数。是这样的:

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


1  

In the code above, function f() has no way of knowing how many elements were in your original array. It's a feature of the language and there's no way around it. You'll have to pass the length.

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

#5


1  

As the C reference says, you cannot do this unless either the last element is unique or you pass a count of array elements to the function.

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

#6


1  

you have to end the array with a special value and in called function you have to count up to that value that is how strlen() works it counts up to NULL '\0' value.

你必须用一个特殊的值来结束这个数组,在这个函数中,你必须要数到那个值,这就是strlen()的工作方式,它的值是NULL '\0'值。

#7


1  

Note that in main(), array refers to the actual array and so sizeof() gives the required answer.

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

But when you pass it as function parameter,you are actually passing the address of the first element of the array which is stored in the pointer variable 'array'.
So now sizeof() gives the size of pointer variable which is why it differs from actual answer.

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

Possible solution can be to

可能的解决办法是

1.Declare the array globally

1。全球声明数组

2.Pass the array size as function parameter Hope it helps!

2。将数组大小作为函数参数传递,希望有帮助!

#8


0  

You can't tell number of elements in an array in C consistently. Specially if you pass the array around through pointers.

在C语言中,你不能始终分辨数组中的元素的数量。特别是如果你通过指针传递数组。

Usually, if you must use array size in a function, pass it as a parameter to it.

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

#9


0  

When you use sizeof in main, it's evaluating the array, and gives the size of the actual array.

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

When you use sizeof in f, you've passed the name of the array as an argument to a function, so it has decayed to a pointer, so sizeof tells you about the size of a pointer.

当你在f中使用sizeof时,你已经将数组的名字作为参数传递给一个函数,所以它已经衰减到一个指针,所以sizeof告诉你一个指针的大小。

Generally speaking, if you pass an array to a function, you need to either write the function to only work with one specific size of array, or explicitly pass the size of array for it to work with on a particular invocation.

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

#10


0  

You may use a format for your array. I am using string elements, it should work for struct.

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

in main ( )

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

You should get the output:

你应该得到输出:

Algo: des 
   :
   :
Algo: zlib 

There are 38 algos in the check list.

Alternatively, if you do not want to use the NULL , do this instead:

或者,如果您不想使用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


0  

As an example to your solution:

作为你解决方案的一个例子:

Given

鉴于

struct contain {
char* a;        //
int allowed;    //

struct suit {
   struct t {
          char* option;
          int count;
   } t;

   struct inner {
          char* option;
          int count;
   } inner;
} suit;
};

// eg. initialized

/ /。初始化

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

};

in main()

在main()

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

gives you the correct answer.

给你正确的答案。

#1


19  

You can't.

你不能。

You have to pass the size to the function, eg:

你必须把尺寸传递给这个函数。

void f(myStruct* array, size_t siz);

Also notice that in f array is a pointer, while in main it is an array. Arrays and pointers are different things.

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

#2


10  

In f array is a pointer, in main array is an array.

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

#3


1  

You must pass that data as a separate parameter to the function. In C and C++ as soon as an array is passed to a function the array degenerates into a pointer. Pointers have no notion of how many elements are in the array they point to.

必须将数据作为单独的参数传递给函数。在C和c++中,数组一旦传递给函数,数组就会退化为指针。指针不知道它们指向的数组中有多少个元素。

A common way to get the size is to declare the array and then immediately get the array element count by dividing the total size by the size of one element. Like this:

获取大小的一种常见方法是声明数组,然后通过将总大小除以一个元素的大小立即获得数组元素计数。是这样的:

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


1  

In the code above, function f() has no way of knowing how many elements were in your original array. It's a feature of the language and there's no way around it. You'll have to pass the length.

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

#5


1  

As the C reference says, you cannot do this unless either the last element is unique or you pass a count of array elements to the function.

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

#6


1  

you have to end the array with a special value and in called function you have to count up to that value that is how strlen() works it counts up to NULL '\0' value.

你必须用一个特殊的值来结束这个数组,在这个函数中,你必须要数到那个值,这就是strlen()的工作方式,它的值是NULL '\0'值。

#7


1  

Note that in main(), array refers to the actual array and so sizeof() gives the required answer.

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

But when you pass it as function parameter,you are actually passing the address of the first element of the array which is stored in the pointer variable 'array'.
So now sizeof() gives the size of pointer variable which is why it differs from actual answer.

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

Possible solution can be to

可能的解决办法是

1.Declare the array globally

1。全球声明数组

2.Pass the array size as function parameter Hope it helps!

2。将数组大小作为函数参数传递,希望有帮助!

#8


0  

You can't tell number of elements in an array in C consistently. Specially if you pass the array around through pointers.

在C语言中,你不能始终分辨数组中的元素的数量。特别是如果你通过指针传递数组。

Usually, if you must use array size in a function, pass it as a parameter to it.

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

#9


0  

When you use sizeof in main, it's evaluating the array, and gives the size of the actual array.

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

When you use sizeof in f, you've passed the name of the array as an argument to a function, so it has decayed to a pointer, so sizeof tells you about the size of a pointer.

当你在f中使用sizeof时,你已经将数组的名字作为参数传递给一个函数,所以它已经衰减到一个指针,所以sizeof告诉你一个指针的大小。

Generally speaking, if you pass an array to a function, you need to either write the function to only work with one specific size of array, or explicitly pass the size of array for it to work with on a particular invocation.

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

#10


0  

You may use a format for your array. I am using string elements, it should work for struct.

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

in main ( )

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

You should get the output:

你应该得到输出:

Algo: des 
   :
   :
Algo: zlib 

There are 38 algos in the check list.

Alternatively, if you do not want to use the NULL , do this instead:

或者,如果您不想使用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


0  

As an example to your solution:

作为你解决方案的一个例子:

Given

鉴于

struct contain {
char* a;        //
int allowed;    //

struct suit {
   struct t {
          char* option;
          int count;
   } t;

   struct inner {
          char* option;
          int count;
   } inner;
} suit;
};

// eg. initialized

/ /。初始化

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

};

in main()

在main()

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

gives you the correct answer.

给你正确的答案。