C: finding the number of elements in an array[]

前端 未结 11 1827
陌清茗
陌清茗 2020-12-16 14:31

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

int main(void) {
  myStruct array[] = { struct1, struct2         


        
相关标签:
11条回答
  • 2020-12-16 14:55

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

    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.

    Possible solution can be to

    1.Declare the array globally

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

    0 讨论(0)
  • 2020-12-16 14:56

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

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

    0 讨论(0)
  • 2020-12-16 14:59

    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.

    0 讨论(0)
  • 2020-12-16 15:01

    You may use a format for your array. I am using string elements, it should work for 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 ( )

    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:

    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);
    
    0 讨论(0)
  • 2020-12-16 15:02

    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.

    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]);
    
    0 讨论(0)
  • 2020-12-16 15:02

    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.

    0 讨论(0)
提交回复
热议问题