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)

6
  • 1
    "I have an array of pointers to integers" What you actually have is a pointer to a pointer to int. Commented Oct 11, 2015 at 21:40
  • 2
    yeah I know that. but how is this relevant to the question? Commented Oct 11, 2015 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!! Commented Oct 11, 2015 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.) Commented Oct 11, 2015 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" ? Commented Oct 11, 2015 at 21:54

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.