Length of array of struct

Hello,

I'm trying to make an application and I need the length of the array of a struct.
Like this:

1
2
3
4
5
6
7
8
9
10
struct myStruct
{
    int integer;
};

myStruct struct[] = 
{
    1,
    2,
};    //I want to get how many ints are in the array 
What you are defining on lines 7 to 10 is an array of integers. Not an array of structures.

I need the length of the array of a struct
I want to get how many ints are in the array


which one?
Last edited on
If you want to calculate amount of elements in the array, you can use safe array size template:
1
2
3
4
5
template <typename T, std::size_t N>
std::size_t array_size(T(&)[N])
{
	return N;
}
Other common approcahes like sizeof(arr)/sizeof(arr[0]) are dangerous, because they will not fail when they are passed something which is not an array, but produce wrong results.

What you are defining on lines 7 to 10 is an array of integers. Not an array of structures.
It is actually is array of structures with illegal name (struct is a reserved word) and incorrect initialization list (individual structure initializers should be in braces)
Topic archived. No new replies allowed.