1

I create a dynamic array of pointers like so:

int ** A;
A = new int*[10];

How do I create a dynamic array of const pointers? Where does the const go?

2
  • 1
    You don't do that in C...you might in C++. Which language are you really using? Also, you might want to consider how you'd initialize those constant pointers to something useful. Once they're allocated, you can't change them, so ...? Commented Jul 2, 2013 at 3:16
  • C++. Ok, so I want an array of pointers where I can't modify the objects they're pointing to. Commented Jul 2, 2013 at 3:22

1 Answer 1

2

Courtesy of cdecl.org:

A = new int * const bar[10];

That will give you an array 10 long of pointers that can't be set to anything. You should also declare A as:

int * const *A;

Otherwise you'll have const casting problems.

Or perhaps you want an array of 10 pointers to constant int:

a = new const int *a[10];
Sign up to request clarification or add additional context in comments.

4 Comments

The problem is const objects must be initialized at declaration.
@JesseGood - Yep. Don't know what OP is really after.
Gah my head is about to explode. I'm just after an array of pointers where I can't modify the objects they are pointing to.
@Frank - Then it's "array of pointer to const T", not "array of const pointer to T" :) If your head is about to explode, you're in good company. There's a reason a tool like cdecl was invented--everyone needs it!

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.