It is not possible to find the number of elements in a dynamically created array. For a non-dynamic array, you can use sizeof(array)/sizeof(type) . However, this is not as useful as it seems:
void f( int a[] ) {
This is because arrays decay into pointers when passed to functions. Therefore, in both cases, you probably need to remember the size of the array and pass it to the functions as a separate parameter. Thus, the function of summing the array (for example) will look like this:
int sum( int a[], int n ) { int total = 0, i; for ( i = 0; i < n; i++ ) { total += a[i]; } return total; }
anon
source share