0

Say I have an array of pointers to integers (i.e each element is a pointer to int)

int** ptrArray;

and I want to prevent changes to the integers pointed by the entries of the array

where do I need to put the const?

1. const int ** ptrArray
2. int const ** ptrArray
3. int *const*  ptrArray
4. int ** const ptrArray

are there any rules for this? like "first const protects data", "second const protects the pointer" and so on?

Is there any logic behind the location of the const? any connection between where to put and what it protect?

this is very a confusing issue for me and I would really appriciate if someone can give me any guide or link to where I can read more about how and where to use the const based on what I want to protect (in case I need to use const in a 3dimensional array or so)

Adam
  • 464
  • 6
  • 16
  • 1
    "I have an array of pointers to integers" What you actually have is a pointer to a pointer to `int`. – juanchopanza Oct 11 '15 at 21:40
  • 2
    yeah I know that. but how is this relevant to the question? – Adam Oct 11 '15 at 21:44
  • 5
    It's absolutely relevant to your understanding of arrays and pointers (and the language more generally), which is a key component of the question and its answers. Don't shrug it off. If you "know that", don't get it wrong in the question!! – Lightness Races in Orbit Oct 11 '15 at 21:46
  • 1
    Then you know wrong. Pay attention. An array is not a pointer (although confusingly the language may make is seem so sometimes.) – juanchopanza Oct 11 '15 at 21:51
  • juanchopanza I was taught that an array is a pointer.. can you please clarify what you mean when you say "array is not a pointer" ? – Adam Oct 11 '15 at 21:54
  • I mean it is not a pointer. It is an array. Pointers point to things. Arrays are contiguous sequences of things. See http://stackoverflow.com/questions/3959705/arrays-are-pointers – juanchopanza Oct 11 '15 at 21:57

3 Answers3

5

are there any rules for this? like "first const protects data", "second const protects the pointer" and so on?

Yes: const applies to the left, unless there's nothing there, then it applies to the right.

Therefore, this:

int const** ptrArray;

but, as a special case, this is equivalent:

const int** ptrArray;

This means the common pattern const int x; is actually int const x; in disguise.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
3

You read modifiers starting from the variable.

e.g.

3. int *const*  ptrArray
  • ptrArray is a variable.
  • *ptrArray denotes a pointer to something
  • const* ptrArray denotes a pointer to something that is constant
  • *const* ptrArray denotes a pointer to a constant pointer to something

So int *const* ptrArray; declares a pointer to a constant pointer to a (nonconstant) int


The final technicality you'd need to know to understand all the examples is that int const and const int are two different ways to denote the same type.

1

http://cdecl.org reports (more or less, one was an invalid syntax error):

const int ** ptrArray:  declare ptrArray as pointer to pointer to const int
int const ** ptrArray:  declare ptrArray as pointer to pointer to const int
int * const * ptrArray: declare ptrArray as pointer to const pointer to int
int ** const ptrArray: declare ptrArray as const pointer to pointer to int

So you're looking for either of:

const int ** ptrArray
int const ** ptrArray
Bill Lynch
  • 80,138
  • 16
  • 128
  • 173