I'm still confusing where to place const in pointers with more than one indirection. Can someone clarify?
E.g. right now I need a pointer to const pointer, meaning such a variable int **ppTargets that I can assign int *pTargets variable to it, like:
int foo(int **ppTargets) {
int *pTargets = /* calculate here */;
*ppTargets = pTargets;
return 37; // just e.g.
}
The above code lacks const. So in foo I want pTargets to point to constant memory and be unassignable after initialization (so that one cannot write e.g. pTargets++), that would be int const *const pTargets = /* assigned once */. Next I want to declare ppTargets that ppTargets itself can be assigned, but then *ppTargets can only be read.
In the other words, in the caller code I want:
int const* pTargets;
foo(&pTargets);
I tried to declare foo as follows, but get an error you cannot assign to a variable that is const:
int foo(int *const *const ppTargets)
typedefto build up from your simple types to the ones you actually want to useSo I want pTargets to point to constant memory and be const itself, that would be int const *const pTargets... thenIn the other words, in the calling code I want: int const* pTargets;So, which is it? IspTargetssupposed to be const or not?pTargetsand**pTargets, but not*pTargets. I don't see a contradiction yet, let me think what could be misunderstood...I want to forbid assignment to pTargetscontradicts withint const* pTargetsbecauseint const*can be assigned.