The reason why OP's signature
func(const double ** array){
// computations using array
}
generates an error when a double ** is passed as an argument, lies in the rules of qualification conversions.
Quoting https://en.cppreference.com/w/cpp/language/implicit_conversion (emphasis mine):
Qualification conversions
- A prvalue of type pointer to cv-qualified type T can be converted to a prvalue pointer to a more cv-qualified same type T (in other words, constness and volatility can be added).
- [...]
"More" cv-qualified means that
- a pointer to unqualified type can be converted to a pointer to const;
- [...]
For multi-level pointers, the following restrictions apply: a multilevel pointer P1 which is cv10-qualified pointer to cv11-qualified pointer to ... cv1n-1-qualified pointer to cv1n-qualified T is convertible to a multilevel pointer P2 which is cv20-qualified pointer to cv21-qualified pointer to ... cv2n-1-qualified pointer to cv2n-qualified T only if
- the number of levels n is the same for both pointers;
- if there is a const in the cv1k qualification at some level (other than level zero) of P1, there is a const in the same level cv2k of P2;
- [...]
- if at some level k the P2 is more cv-qualified than P1, then there must be a const at every single level (other than level zero) of P2 up until k: cv21, cv22 ... cv2k.
- [...]
- level zero is addressed by the rules for non-multilevel qualification conversions.
char** p = 0;
const char** p1 = p; // error: level 2 more cv-qualified but level 1 is not const
const char* const * p2 = p; // OK: level 2 more cv-qualified and
// const added at level 1
Note that in the C programming language, const/volatile can be added to the first level only:
char** p = 0;
char * const* p1 = p; // OK in C and C++
const char* const * p2 = p; // error in C, OK in C++
So, to enforce constness, the signature needs to be changed into
void func(double const * const * array) {
// ... ^^^^^
}
That said, I strongly suggest to change the overall design and avoid that dynamically allocated jagged array, if possible.
std::vector. Usingstd::vectoryou would not have the problem you have now (which I know there are duplicates of, even though I can't seem to find it).