How do I pass an entire array pointer as const such that the values cannot be changed? I am passing in an array to a function like this:
double myArr[] { 1, 2, 3, 4, 5 };
double* pArr = myArr;
double myVal = MyFunc(pArr, 5);
MyFunc’s header right now is:
MyFunc(double* pArr, int length)
I want to make sure that the function cannot modify the values inside the array at all.
How do I do this?
Change your function signature to:
Note that this is equivalent to:
Which one you prefer is a matter of pure taste (I prefer the first, as I find it more readable — it’s the items in
pArrthat are const, not the pointer itself).