How to get size of array in array of structs with C

1 Answer

0 votes
#include <stdio.h>

typedef struct {
    int arr[6];
    char ch;
} ST;

int main(void) {

    static const ST s[] = { {{ 3, 5, 9 }, 'z'}, {{7}, 'a'}, {{ 4, 8 }, 'x'} };

    size_t struct_size = sizeof s / sizeof s[0];
    size_t struct_array_size = sizeof s[0].arr / sizeof(int);

    printf("struct_size = %zu\n", struct_size);
    printf("struct_array_size = %zu", struct_array_size);

    return 0;
}




/*
run:

struct_size = 3
struct_array_size = 6

*/

 



answered Apr 24, 2023 by avibootz

Related questions

1 answer 115 views
115 views asked Apr 24, 2023 by avibootz
1 answer 171 views
2 answers 278 views
278 views asked May 8, 2019 by avibootz
1 answer 143 views
1 answer 104 views
...