### get size of any type array Does anyone know how to find the length of an arbitrary type of array? All I have found online were how to find the length of an int array.
In my case I am trying to find the length of a string array. ``1234567891011`` ``````#define ARRAY_SIZE(array) (sizeof((array))/sizeof((array))) int main() { short arr_int[]={4,1,2,4,5}; char arr[]={"abc"}; cout <

Note that this method doesn't work if array is allocated dynamically:
 ``12`` `````` char *dyn_arr=new char; cout < Thank you for your help, but can you explain to me why do you divide by sizeof((array))? `sizeof(array)` returns array size in bytes. `sizeof(array)` returns size of single element in array. To get number of elements in array you must divide arrays's size by size of single element.

` int arr_int[]={4,1,2,4,5};`

` sizeof(arr_int)` == `sizeof(int)` == 4 bytes
`sizeof(arr_int`) == 20 bytes
20/4==5 elements in array
Last edited on Null's solution is error-prone. I don't recommend it. macros in general are evil for this reason (and others)

The templated solution is superior.

Here's a compare/contrast:

 ``123456789101112131415161718192021222324`` ``````// bad #define ARRAY_SIZE(array) (sizeof((array))/sizeof((array))) // good template inline unsigned arraysize(const T (&v)[S]) { return S; } int main() { // different kinds of arrays, all size 5 int normalarray[] = {1,2,3,4,5}; std::vector vec(5); int* dynamic = new int; // ARRAY_SIZE will only work for normalarray: cout << ARRAY_SIZE(normalarray); // prints 5 as expected cout << ARRAY_SIZE(vec); // prints unreliable garbage cout << ARRAY_SIZE(dynamic); // prints unreliable garbage // on the other hand, arraysize will give you an error if you use it incorrectly: cout << arraysize(normalarray); // prints 5 as expected cout << arraysize(vec); // gives you a compiler error cout << arraysize(dynamic); // gives you a compiler error }``````
Last edited on @Disch: Could you explain how it works? Why `unsigned S` is a template parameter? And how its value equals to array size? The code is passing an array by reference. Here's a way to make it easier to understand:

 ``1234567891011121314151617181920`` ``````typedef int int5; // int5 foo; is now the same as int foo; void func(const int5& v) // the typedef makes this easier to understand { // a function that takes an array of 5 ints by reference } int main() { int four; int five; int six; func(four); // error int is not int func(five); // OK func(six ); // error int is not int int* dynamic = new int; func(dynamic); // error, int* is not the same as int }``````

The int5 typedef makes the function easier to understand, but you can do it without the typedef:

 ``1234`` ``````void func(const int (&v)) { // same thing as above }``````

If you template it, the function can accept arrays of any size:

 ``1234567891011121314151617181920`` ``````template void func(int (&v)[S]) { cout << S; } int main() { int four; int five; // verbose, specifying the size in the

All I do from there is just template the type as well, so it can work with arrays of any type instead of just arrays of ints. Now I understand, thanks. @Disch: What does (&v) means? This is the first time I've seen it. By the way, thank you for the explanation. It means that v is a reference.
Topic archived. No new replies allowed.