For ex. I have a function
void len(int *A){ // or we have its reference
// can I find the length of array here or
// we just need to pass the length along as the parameter
}
For ex. I have a function
void len(int *A){ // or we have its reference
// can I find the length of array here or
// we just need to pass the length along as the parameter
}
can I find the length of array here or we just need to pass the length along as the parameter
You can't find the length once the array has decayed into a pointer so one option is to supply the length as a parameter as you said yourself.
Another option is to not let it decay into a pointer:
template<size_t N>
void len(int(&A)[N]) {
// N is the length
}
Or you could use a std::vector<int>
:
void len(std::vector<int>& A) {
// A.size() returns the length
}